JTable:清除行选择时删除单元格周围的边框
JTable: Remove border around cell when clearing row selection
我有一个 JTable
,我想允许通过单击 table 的空白部分来取消选择所有行。到目前为止效果很好。但是,即使我调用 table.clearSelection();
,table 仍然显示先前启用的单元格周围的边框(参见示例中的单元格 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);
}
}
[edit] 尝试添加此处提到的 table.getColumnModel().getSelectionModel().clearSelection();
。但这也无济于事。
您的问题:即使选择丢失,您的 table 单元格仍具有焦点,因此它会通过显示加粗的边框来自行显示。会心
一种可能的解决方案是创建您自己的渲染器,当单元格失去选择时移除单元格的焦点。例如:
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (!isSelected) {
hasFocus = false;
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
});
Tried to add table.getColumnModel().getSelectionModel().clearSelection();
table.clearSelection()
方法调用该方法和 TableColumnModel
的 clearSelection()
方法。
除了清除选择之外,您还需要重置选择模型的 "anchor and lead" 个索引:
table.clearSelection();
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.setAnchorSelectionIndex(-1);
selectionModel.setLeadSelectionIndex(-1);
TableColumnModel columnModel = table.getColumnModel();
columnModel.getSelectionModel().setAnchorSelectionIndex(-1);
columnModel.getSelectionModel().setLeadSelectionIndex(-1);
现在,如果您使用箭头键,焦点将转到 (0, 0),因此您会丢失有关最后一个被单击单元格的信息。
如果只清除选择模型,那么行信息会丢失,但列信息会保留。
尝试清除一个或两个模型以获得您想要的效果。
我有一个 JTable
,我想允许通过单击 table 的空白部分来取消选择所有行。到目前为止效果很好。但是,即使我调用 table.clearSelection();
,table 仍然显示先前启用的单元格周围的边框(参见示例中的单元格 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);
}
}
[edit] 尝试添加此处提到的 table.getColumnModel().getSelectionModel().clearSelection();
。但这也无济于事。
您的问题:即使选择丢失,您的 table 单元格仍具有焦点,因此它会通过显示加粗的边框来自行显示。会心 一种可能的解决方案是创建您自己的渲染器,当单元格失去选择时移除单元格的焦点。例如:
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (!isSelected) {
hasFocus = false;
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
});
Tried to add table.getColumnModel().getSelectionModel().clearSelection();
table.clearSelection()
方法调用该方法和 TableColumnModel
的 clearSelection()
方法。
除了清除选择之外,您还需要重置选择模型的 "anchor and lead" 个索引:
table.clearSelection();
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.setAnchorSelectionIndex(-1);
selectionModel.setLeadSelectionIndex(-1);
TableColumnModel columnModel = table.getColumnModel();
columnModel.getSelectionModel().setAnchorSelectionIndex(-1);
columnModel.getSelectionModel().setLeadSelectionIndex(-1);
现在,如果您使用箭头键,焦点将转到 (0, 0),因此您会丢失有关最后一个被单击单元格的信息。
如果只清除选择模型,那么行信息会丢失,但列信息会保留。
尝试清除一个或两个模型以获得您想要的效果。