How to remove focus from one jtable cell, if I click the another Jtable cell?

2k views Asked by At

I am using two JTables. If I click the first table cell, the cell in that table getting focused. After that, if I click the second table, the second table cell getting focused. But the first table also having the focus. How to remove the focus in the first JTable Cell. Help me. Thanks in advance.

2

There are 2 answers

0
Kishan Bheemajiyani On BEST ANSWER

Well on the Table Click event you can get the table Cell which clicked..

public void mouseClicked(java.awt.event.MouseEvent event) {
    int row = theTable.rowAtPoint(event.getPoint());
    int col = theTable.columnAtPoint(event.getPoint());

    table.changeSelection(row, col, false, false);
    table.requestFocus();
}

try it or jtable cell having by default that behaviour if u use netbeans.

0
George On

Here's another way, compatible with different kinds of listeners:

//Listener for Table 2
if(table1.isEditing()){ //To prevent Null Pointer Exception
    table1.getCellEditor().stopCellEditing();
}

...

//Listener for Table 1
if(table2.isEditing()){ //To prevent Null Pointer Exception
    table2.getCellEditor().stopCellEditing();
}