我正在为 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
;但这会导致抛出异常错误。正如我所说,构造函数中不应该有任何参数。有人能找到解决这个问题的方法吗?提前致谢!