通过单击表单的其他空格来清除JTable选择的最佳方法是什么? 我试过这个:
table1.addFocusListener(new MyTableFocusListener());
...
public class MyTableFocusListener implements FocusListener {
@Override
public void focusLost(FocusEvent e)
{
table1.getSelectionModel().clearSelection();
}
@Override
public void focusGained(FocusEvent e)
{
}
}
但有例外:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
答案 0 :(得分:2)
通过点击清除JTable选择的最佳方法是什么 形式的其他空间?
这听起来像MouseListener而不是FocusListener的工作。让我们说你的桌子放在你表格的某个面板上。例如:
final JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
JPanel formContent = new JPanel();
formContent.add(scrollPane);
您可以在此面板中添加MouseListener
并使用JTable.clearSelection()
方法,如@MadProgramer建议:
formContent.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(!table.contains(e.getPoint())) { // contains(Point point) method is inherited from java.awt.Component
table.clearSelection();
}
}
});
看看: