获取 itemStateChanged 上 TableCellRenderer 的行

发布于 2024-12-27 14:27:06 字数 1319 浏览 2 评论 0 原文

我有一个用于 JTable 的自定义 TableCellRenderer (ValueRenderer),单元格是一个 Checkbox

我已将 ItemListener 附加到 valueRenderer,以监听复选框的状态更改(选中/取消选中),如此 示例

我的问题是,在 itemStateChanged(ItemEvent e) 内,我不知道如何获取包含此复选框的行,因为知道 ItemEvent 源是 ValueRenderer。

你能帮助我吗?

这是我的一些代码:

自定义 TableCellRender:

public class ValueRenderer extends JCheckBox implements TableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        this.setSelected((Boolean) value);
        return this;
    }

} 

ItemListener:

public class TableRowCheckBoxListener implements ItemListener {

    private JTable hqlRequestTable;

    public TableRowCheckBoxListener(JTable hqlRequestTable) {
        this.hqlRequestTable = hqlRequestTable;
    }

    @Override
    public void itemStateChanged(ItemEvent e) {

        /*How do I get the row which contains the checkbox clicked knowing that :
            e.getSource() == ValueRenderer
            e.getItem() == ValueRender
        */
    }

}

I have a custom TableCellRenderer (ValueRenderer) for a JTable, the cell is a Checkbox.

I have attached an ItemListener to the valueRenderer to listen to the checkbox's state change (selected/deselected) as mentioned by this example.

My problem is that inside the itemStateChanged(ItemEvent e), I do not know how to get the row in which this checkbox is contained knowing that the ItemEvent source is the ValueRenderer.

Can you help me?

Here is some of my code:

Custom TableCellRender:

public class ValueRenderer extends JCheckBox implements TableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        this.setSelected((Boolean) value);
        return this;
    }

} 

ItemListener:

public class TableRowCheckBoxListener implements ItemListener {

    private JTable hqlRequestTable;

    public TableRowCheckBoxListener(JTable hqlRequestTable) {
        this.hqlRequestTable = hqlRequestTable;
    }

    @Override
    public void itemStateChanged(ItemEvent e) {

        /*How do I get the row which contains the checkbox clicked knowing that :
            e.getSource() == ValueRenderer
            e.getItem() == ValueRender
        */
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

星軌x 2025-01-03 14:27:06

如果您想知道表中的某些值何时发生变化,则不得在渲染器上注册侦听器。您必须在表模型上注册一个侦听器:这是保存表显示的数据的位置,也是在数据发生任何更改时触发事件的对象。

另一种方法是使用包含 bean 列表的自定义表模型,让表模型修改它所保存的 bean 的属性,并让 bean 在属性更改时触发属性更改事件。然后,您将在 bean 本身上注册侦听器,而不是注册表模型侦听器(但请注意,表模型仍然必须触发表模型事件)。

If you want to know when some value changes in your table, you must not register a listener on the renderer. You must register a listener on the table model: that's where the data displayed by the table is held, and that's the object which fires an event if anything changes in the data.

The alternative is to use a custom table model consisting in a list of beans, have the table model modify the properties of the beans it holds, and have the bean fire a property change event when a property changes. You'll then register listeners on the beans themselves rather than registering a table model listener (note that the table model still has to fire table model events, though).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文