您可以简单地从 UI 的角度扩展DefaultTableCellRenderer
并假装该单元格没有“聚焦”。
我使用以下渲染器删除了边框:
private static class BorderLessTableCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(
final JTable table,
final Object value,
final boolean isSelected,
final boolean hasFocus,
final int row,
final int col) {
final boolean showFocusedCellBorder = false; // change this to see the behavior change
final Component c = super.getTableCellRendererComponent(
table,
value,
isSelected,
showFocusedCellBorder && hasFocus, // shall obviously always evaluate to false in this example
row,
col
);
return c;
}
}
您可以像这样在 JTable 上设置它:
table.setDefaultRenderer( Object.class, new BorderLessTableCellRenderer() );
或者,对于字符串:
table.setDefaultRenderer( String.class, new BorderLessTableCellRenderer() );
这有点像 hack,因为它只是重用原始渲染器并假装焦点/选定的单元格不是,但它应该让你开始。