在 DefaultCellEditor 派生实例完成其工作后更新单元格渲染器

发布于 2024-07-21 21:30:02 字数 1061 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

渡你暖光 2024-07-28 21:30:02

在您的表模型中,每当您的值发生变化时,您都必须触发适当的事件。 如果您的模型继承自 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 several fireXXX methods. My guess is you should call them from setValueAt method.

If you know exact column and row - you can call fireTableCellUpdated, otherwise you can you probably have to use fireTableChanged since you have to update different column.

And of course you renderer should properly render new value.

_畞蕅 2024-07-28 21:30:02

在那里扩展 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.

亚希 2024-07-28 21:30:02

我想它可以帮助遇到类似问题的人,使 true 单选按钮在一行中唯一,您必须扩展 DefaultTableModel 来修改其行为,尤其是 setValueAt 方法

干杯


/**
 * When <code>column</code> is the column that contains the Boolean (in fact the radio button):
 * If aValue == false and that it had a previous value set to true we don't do anything
 * If aValue == true and that it had a previous value set to false, we set all the other booleans to false and this one to true
 */
@Override
public void setValueAt(Object aValue, int row, int column) {
    if (column == colonneBoutonradio)
    {
        if (((Boolean)aValue && !(Boolean)super.getValueAt(row, column)))
            for (int i = 0; i < this.getRowCount(); i++)
                // i==row permet de vérifier si la ligne courante est celle à modifier (et donc celle à mettre à true)
                super.setValueAt(i==row, i, colonneBoutonradio);
    }
    else
        super.setValueAt(aValue, row, column);
}

I guess it could help people with a similar problem, make a true radiobutton unique in a row, you'll have to extend the DefaultTableModel to modify its behaviour especially the setValueAt method

Cheers


/**
 * When <code>column</code> is the column that contains the Boolean (in fact the radio button):
 * If aValue == false and that it had a previous value set to true we don't do anything
 * If aValue == true and that it had a previous value set to false, we set all the other booleans to false and this one to true
 */
@Override
public void setValueAt(Object aValue, int row, int column) {
    if (column == colonneBoutonradio)
    {
        if (((Boolean)aValue && !(Boolean)super.getValueAt(row, column)))
            for (int i = 0; i < this.getRowCount(); i++)
                // i==row permet de vérifier si la ligne courante est celle à modifier (et donc celle à mettre à true)
                super.setValueAt(i==row, i, colonneBoutonradio);
    }
    else
        super.setValueAt(aValue, row, column);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文