在 DefaultCellEditor 派生实例完成其工作后更新单元格渲染器
我使用 JTable
它有自己的单元格渲染器和单元格编辑器。
比如说,该表包含 2 列和 x 行:
第一列包含一个布尔值、它自己的单元格渲染和单元格编辑器(单选按钮)
第二列包含一个字符串值,它自己的单元格渲染器:当当前行的第一列设置为 true(选中单选按钮)时,它会使其变为粗体。
所有值均由编辑器正确更新,但第二行不会变为粗体当单选按钮设置为 true 时...
我必须检查不同行中的单选按钮才能查看更改
我可以在哪里触发这些更改?
干杯并感谢您的帮助
RadiobuttonTableCellEditor.java
public class RadiobuttonTableCellEditor extends DefaultCellEditor
implements ItemListener {
JRadioButton rb = new JRadioButton();
public RadiobuttonTableCellEditor(JCheckBox pCheckBox) {
super(pCheckBox);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value == null)
return null;
rb.addItemListener(this);
rb.setSelected((Boolean)value);
return rb;
}
public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
public Object getCellEditorValue() {
rb.removeItemListener(this);
return rb.isSelected();
}
}
I use a JTable
which has its own cell renderer and cell editor.
Say, this table contains 2 columns and x rows:
The first column contains a boolean value, its own cell rendering and cell editor (a radiobutton)
The second column contains a string value, its own cell renderer: it makes it bold when the first column of the current row is set to true (radiobutton checked)
All the values are correctly updated by the editor but the 2nd row does not become bold when the radio button is set to true...
I have to check a radio button from a different row to see the changes
Where can I fire thoses changes ?
Cheers and thanks for your help
RadiobuttonTableCellEditor.java
public class RadiobuttonTableCellEditor extends DefaultCellEditor
implements ItemListener {
JRadioButton rb = new JRadioButton();
public RadiobuttonTableCellEditor(JCheckBox pCheckBox) {
super(pCheckBox);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value == null)
return null;
rb.addItemListener(this);
rb.setSelected((Boolean)value);
return rb;
}
public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
public Object getCellEditorValue() {
rb.removeItemListener(this);
return rb.isSelected();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的表模型中,每当您的值发生变化时,您都必须触发适当的事件。 如果您的模型继承自
AbstractTableModel
,您可以使用多个fireXXX
方法。 我的猜测是你应该从 setValueAt 方法调用它们。如果您知道确切的列和行 - 您可以调用
fireTableCellUpdated
,否则您可能必须使用fireTableChanged
因为您必须更新不同的列。当然,您的渲染器应该正确渲染新值。
In your table model whenever your value changes you have to fire appropriate event. If your model is inherited from
AbstractTableModel
you can use severalfireXXX
methods. My guess is you should call them fromsetValueAt
method.If you know exact column and row - you can call
fireTableCellUpdated
, otherwise you can you probably have to usefireTableChanged
since you have to update different column.And of course you renderer should properly render new value.
在那里扩展
DeafultCellEditor
似乎没有任何意义。 实现这样的监听器接口也不是一个好主意。渲染器作为薄层效果最好。 如果另一个单元格发生变化,则需要反映在表模型中,该模型应该触发相关的更新事件。
It doesn't seem to make any sense to extend
DeafultCellEditor
there. Implementing a listener interface like that is also not a great idea.Renderers work best as a thin layer. If another cell should change, then that needs to be reflected in the table model which should fire a relevant update event.
我想它可以帮助遇到类似问题的人,使
true
单选按钮在一行中唯一,您必须扩展DefaultTableModel
来修改其行为,尤其是setValueAt
方法干杯
I guess it could help people with a similar problem, make a
true
radiobutton unique in a row, you'll have to extend theDefaultTableModel
to modify its behaviour especially thesetValueAt
methodCheers