Swing JComboBox作为CellRenderer未设置正确的值
我在JTable中使用JComboBox作为CellRenderer。Swing JComboBox作为CellRenderer未设置正确的值,swing,jtable,jcombobox,tablecellrenderer,Swing,Jtable,Jcombobox,Tablecellrenderer,我在JTable中使用JComboBox作为CellRenderer。 一切正常JComboBox显示对应行的正确项 我目前正在处理的问题是,当我在JComboBox(例如第9行)中选择一个新值时,该值设置正确,但当我尝试更改下一行中的值时,JComboBox(例如第10行)会自动设置前一行的值 我创建了一个DropDownCellRenderer类,它扩展了JComboBox并实现了TableCellRenderer,我认为这已经足够了,但似乎DropDownCellRenderer对象对于每
一切正常JComboBox显示对应行的正确项 我目前正在处理的问题是,当我在JComboBox(例如第9行)中选择一个新值时,该值设置正确,但当我尝试更改下一行中的值时,JComboBox(例如第10行)会自动设置前一行的值 我创建了一个DropDownCellRenderer类,它扩展了JComboBox并实现了TableCellRenderer,我认为这已经足够了,但似乎DropDownCellRenderer对象对于每一行都是相同的
table.getColumnModel().getColumn( 3 ).setCellRenderer( new DropDownCellRenderer() );
table.getColumnModel().getColumn( 3 ).setCellEditor( new DefaultCellEditor( new DropDownCellRenderer() ) );
如何避免每一行使用相同的对象?听起来您是在组合框本身中保存和显示值,而不是在表的模型中保存和显示值。设置值并保存组合框值时,需要更新模型,这听起来像是在组合框本身中保存和显示值,而不是在表的模型中。设置值并保存组合框值时,需要更新模型,查看渲染器的源代码
值是来自表模型的产品(如果正确实现)。只需将值
设置为选中项,它就可以工作了
isSelected
参数更改其前景色和背景色。代码应该如下所示:
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
查看了渲染器的源代码
值是来自表模型的产品(如果正确实现)。只需将值
设置为选中项,它就可以工作了
isSelected
参数更改其前景色和背景色。代码应该如下所示:
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
您的渲染器实现可能不正确,我们可以看到吗?您可以在此处看到渲染器实现:您的渲染器实现可能不正确,我们可以看到吗?您可以在此处看到渲染器实现: