JTable中列的多个单元格渲染器? [英] Multiple cell renderers for a column in a JTable?
问题描述
假设我有以下JTable,只要按下按钮就会显示:
Let's say I have the following JTable, which is displayed as soon as a button is pressed:
| Name
------+------------
True | Hello World
False | Foo Bar
True | Foo
False | Bar
我想将最初的单元格渲染为JCheckBox ,以及最初为false的所有单元格都不显示任何内容(没有JCheckBox)。用户可以检查或取消检查最初为真的单元格中的JCheckBox,这对我创建的图表有效。
I want to render the cells that were initially true to a JCheckBox, and all cells that were initially false to not display anything (no JCheckBox). The user could check or un-check the JCheckBoxes in the cells that were initially true, which would do something to a chart I created.
现在,我的单元格渲染器显示JCheckBoxes在所有单元格中,包括最初为假的那些(它显示那些没有复选标记的JCheckBox),但我想不在后者中显示任何内容。这是我的代码:
Right now, my cell renderer displays JCheckBoxes in all cells, including those that were initially false (it displays those JCheckBoxes without check marks), but I want to not display anything in the latter. Here is my code:
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;
}
}
在 if
声明,我尝试使用 this.setVisible(false)
然后再使用 NoCheckBoxCellRenderer
,但它不是'工作。我正在考虑使用多个单元格渲染器来完成此任务。有可能这样做吗?任何建议都将不胜感激!
In the if
statement, I tried using this.setVisible(false)
before using NoCheckBoxCellRenderer
, but it wasn't working. I'm thinking about using multiple cell renderers to accomplish this task. Would it be possible to do so? Any advice would be greatly appreciated!
推荐答案
将Boolean.TRUE存储为真值。然后为false值存储一个空String。然后,您将需要:
Store Boolean.TRUE for the true values. Then store an empty String for the false values. You will then need to:
a)覆盖getCellRenderer(...)方法,以返回单元格中找到的数据的相应渲染器。
a) override the getCellRenderer(...) method to return the appropriate renderer for the data found in the cell.
b)使包含空字符串的单元格不可编辑:
b) make the cells containing the empty string non-editable:
JTable table = new JTable(data, columnNames)
{
public TableCellRenderer getCellRenderer(int row, int column)
{
if (column == 0)
{
Class cellClass = getValueAt(row, column).getClass();
return getDefaultRenderer( cellClass );
}
return super.getCellRenderer(row, column);
}
public boolean isCellEditable(int row, int column)
{
Class cellClass = getValueAt(row, column).getClass();
if (column == 0 && cellClass instanceof Boolean)
{
return true;
}
else
{
return false;
}
return super.isCellEditable(row, column);
}
};
使用这种方法不需要自定义渲染器或编辑器。
Using this approach there is no need for custom renderers or editors.
这篇关于JTable中列的多个单元格渲染器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!