Home > Net >  How to get the index of a column of my jTable?
How to get the index of a column of my jTable?

Time:01-28

What happens is I want to get the index of the header of my column of my jTable when I click, I have tried with:

Tabla.getSelectedColumn() But this only devains the index of the column when I click on the cell.

CodePudding user response:

You can add a mouse listener to the table's column header. In the mouse listener's mouseClicked() method, you can use the getTableHeader().columnAtPoint() method to get the index of the clicked column.

table.getTableHeader().addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        int column = table.getTableHeader().columnAtPoint(e.getPoint());
        System.out.println("Column index: "   column);
    }
});

This will print the index of the clicked column to the console every time a column header is clicked.

Alternatively, you can use Jtable's getSelectedColumn(), which will give you the index of the selected column.

int selectedColumn = table.getSelectedColumn();

This will give you the index of the selected column.

  • Related