当 JTable 中的 table 失去焦点时如何取消选择该行?

How to deselect the row, when lost focus for a table in JTable?

为了select连续,我使用这个代码:

table_2.addMouseListener(new MouseAdapter() {
          @Override
          public void mousePressed(MouseEvent event1) {
          if (event1.getButton() == MouseEvent.BUTTON3) {
                  Point point = event1.getPoint();
                  int column = table_2.columnAtPoint(point);
                  int row = table_2.rowAtPoint(point);
                  table_2.setColumnSelectionInterval(column, column);
                  table_2.setRowSelectionInterval(row, row);
          }
       }
  });

然后,要重置突出显示的行,我使用以下代码:

table_2.addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent arg0) {
       table_2.clearSelection();
    }
});  

但我想知道,是否有任何其他方式重置突出显示的行?
所以我得到了:

table_2.getSelectedRow()==-1

In order to select a row, I use this code:

更简单的方法是使用:

table.changeSelection(row, column, false, false);

Then, to reset the highlighted line, I use this code:

当您单击 table 中的另一个单元格时,不会生成 focusLost(..) 事件,因为焦点仍在 table 上。无需清除选择,因为使用我建议的代码,当您单击另一行时会自动清除选择。

if (event1.getButton() == MouseEvent.BUTTON3) {

不要用"MouseEvent.BUTTON3",别人不知道那是什么意思。而是使用

//if (SwingUtilties.isRightMouseButton( event1 ))
if (SwingUtilities.isRightMouseButton( event1 ))

哪个更容易阅读和理解。