我正在为 JTable 中的 JComboBox 创建单元格渲染器。此类的构造函数不应带任何参数。我有以下 getTableCellRendererComponent 方法的基本代码:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,int row, int column)
{
if (value != null) {
removeAllItems();
value = value_to_string;
addItem(value);
if (isSelected) {
this.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
this.setForeground(table.getForeground());
this.setBackground(table.getBackground());
}
// Select the current value
this.setSelectedItem(value);
}
return this;
}
问题是我将字符串对象数组 (String[]) 作为一个值,而不是一个对象。我尝试使用 String[] value_to_string = (String[]) value
;但这会导致抛出异常错误。正如我所说,构造函数中不应该有任何参数。有人能找到解决这个问题的方法吗?提前致谢!
最佳答案
The problem is that I would have as a value, instead of an Object, an array of String objects (String[]).
那么你的模型中的数据是错误的。 TableModel 应该只包含一个值。它是从组合框中选择的值。 String[] 仅由组合框编辑器使用,而不由渲染器使用。
关于java - JTable 中 JComboBox 的单元格渲染器类,构造函数中不带参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7777070/