假设我有以下 JTable,按下按钮后就会显示:
| Name
------+------------
True | Hello World
False | Foo Bar
True | Foo
False | Bar
我想渲染那些单元格最初对于 JCheckBox 来说是正确的,并且所有单元格都是最初false 表示不显示任何内容(无 JCheckBox)。用户可以选中或取消选中最初为 true 的单元格中的 JCheckBox,这会对我创建的图表执行某些操作。
现在,我的单元格渲染器在所有单元格中显示 JCheckBoxes,包括那些最初为 false 的单元格(它显示那些没有复选标记的 JCheckBoxes),但我不想在后者中显示任何内容。这是我的代码:
protected class CheckBoxCellRenderer extends JCheckBox implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (!(Boolean) tableModel.getValueAt(row, 0)) {
NoCheckBoxCellRenderer renderer = new NoCheckBoxCellRenderer();
return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
this.setSelected((Boolean) tableModel.getValueAt(row, 0));
return this;
}
}
protected class NoCheckBoxCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
this.setVisible(false);
return this;
}
}
In the if
声明,我尝试使用this.setVisible(false)
使用前NoCheckBoxCellRenderer
,但它不起作用。我正在考虑使用多个单元渲染器来完成此任务。可以这样做吗?任何建议将不胜感激!