【发布时间】:2017-04-16 14:06:50
【问题描述】:
我有一个JTable 并希望允许通过单击表格的空白部分来取消选择所有行。到目前为止,这工作正常。但是,即使我调用table.clearSelection();,表格仍然会在先前启用的单元格周围显示边框(请参见示例中的单元格 5):
我也想去掉这个边框(它在 Mac 的原生外观上看起来特别不合适,单元格突然变黑)。
完全工作的最小示例代码:
public class JTableDeselect extends JFrame {
public JTableDeselect() {
Object rowData[][] = { { "1", "2", "3" }, { "4", "5", "6" } };
Object columnNames[] = { "One", "Two", "Three" };
JTable table = new JTable(rowData, columnNames);
table.setFillsViewportHeight(true);
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (table.rowAtPoint(e.getPoint()) == -1) {
table.clearSelection();
}
}
});
add(new JScrollPane(table));
setSize(300, 150);
}
public static void main(String args[]) throws Exception {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
new JTableDeselect().setVisible(true);
}
}
[编辑] 尝试添加这里提到的table.getColumnModel().getSelectionModel().clearSelection();。但这也无济于事。
【问题讨论】: