检查组件何时从 JTable 中删除

发布于 2024-12-18 04:23:31 字数 1521 浏览 3 评论 0原文

我有一个 JTable,它使用自定义渲染器/编辑器保存多个 JPanel。 JPanel 监听另一个对象的变化。现在,当我从 JTable 中删除行时,JPanel 仍然存在于对象的侦听器列表中,因此 JPanel 不会被破坏。

以前,我在另一个 JPanel 中显示了 JPanel,因此当对象被删除时,我可以添加一些代码来在其 removeNotify() 方法中取消注册侦听器。

当 JPanel 位于 JTable 中时,removeNotify() 技巧不起作用,因为它会不断被删除并重新添加到表的渲染器/编辑器中。我尝试使用 addNotify() 将 JPanel 添加为侦听器,但不知何故它没有从侦听器列表中删除。 那么我怎样才能尽可能干净地做到这一点呢?

如何向表中添加行:

public void fillTable()
{
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.setRowCount(0);
    CustomPanel panel = new CustomPanel(getSomeObject());
    model.addRow(new Object[]{panel});
}

自定义面板如何注册为侦听器:

public class CustomPanel extends JPanel implements CustomObjectListener
{
    public CustomPanel(CustomObjet obj)
    {
        obj.addListener(this);
    }

    @Override
    public void CustomObjectEvent(Event evt)
    {
        handle event;
    }
}

如何引发事件:

public class CustomObject
{
    ArrayList<CustomObjectListener> listeners = new ArrayList<CustomObjectListener>();

    public CustomObject()
    {
    }

    public void addListener(CustomObjectListener listener)
    {
        listeners.add(listener);
    }

    public void removeListener(CustomObjectListener listener)
    {
        listeners.remove(listener);
    }

    public void fireEvent(Event evt)
    {
        for (CustomObjectListener listener : listeners)
        {
            listener.CustomObjectEvent(evt);
        }
    }
}

I have a JTable that holds several JPanels using a custom renderer/editor. The JPanel listens to another object for changes. Now when I remove the rows from the JTable the JPanel still exists in the listener list in the object so the JPanel doesn't get destroyed.

Previously I displayed the JPanel in another JPanel so when the object got removed I could add some code to unregister the listener in its removeNotify() method.

The removeNotify() trick doesn't work when the JPanel is in a JTable because it is constantly removed and re-added to the table's renderer/editor. I tried using addNotify() to add the JPanel as a listener, but somehow it doesn't get removed from the listener list.
So how can I do this as clean as possible?

How I add rows to the table:

public void fillTable()
{
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.setRowCount(0);
    CustomPanel panel = new CustomPanel(getSomeObject());
    model.addRow(new Object[]{panel});
}

How the custom panel registers as a listener:

public class CustomPanel extends JPanel implements CustomObjectListener
{
    public CustomPanel(CustomObjet obj)
    {
        obj.addListener(this);
    }

    @Override
    public void CustomObjectEvent(Event evt)
    {
        handle event;
    }
}

How the event is thrown:

public class CustomObject
{
    ArrayList<CustomObjectListener> listeners = new ArrayList<CustomObjectListener>();

    public CustomObject()
    {
    }

    public void addListener(CustomObjectListener listener)
    {
        listeners.add(listener);
    }

    public void removeListener(CustomObjectListener listener)
    {
        listeners.remove(listener);
    }

    public void fireEvent(Event evt)
    {
        for (CustomObjectListener listener : listeners)
        {
            listener.CustomObjectEvent(evt);
        }
    }
}

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

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

发布评论

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

评论(2

风吹短裙飘 2024-12-25 04:23:31

我有一个 JTable,它使用自定义渲染器/编辑器保存多个 JPanel。

DefaultCellEditor 有一个 stopCellEditing() 方法,您应该能够向其中添加代码。

或者您可以将 PropertyChangeListener 添加到表中:

public void propertyChange(PropertyChangeEvent e)
{
    //  A cell has started/stopped editing

    if ("tableCellEditor".equals(e.getPropertyName()))
    {
        if (table.isEditing())
            processEditingStarted();
        else
            processEditingStopped();
    }
}

I have a JTable that holds several JPanels using a custom renderer/editor.

The DefaultCellEditor has a stopCellEditing() method you should be able to add your code to.

Or maybe you can add a PropertyChangeListener to the table:

public void propertyChange(PropertyChangeEvent e)
{
    //  A cell has started/stopped editing

    if ("tableCellEditor".equals(e.getPropertyName()))
    {
        if (table.isEditing())
            processEditingStarted();
        else
            processEditingStopped();
    }
}
小红帽 2024-12-25 04:23:31

我通过创建一个新的 TableModel 并重写 setRowCount() 方法来修复它。

public class CustomTableModel extends DefaultTableModel
{
    @Override
    public void setRowCount(int rowCount)
    {
        if (rowCount < getRowCount())
        {
            for (int i = getRowCount()-1; i >= rowCount; i--)
            {
                ((CustomPanel)getValueAt(i, 0)).removeListeners();
            }
        }
        super.setRowCount(rowCount);
    }
}

I fixed it by creating a new TableModel and overriding the setRowCount() method.

public class CustomTableModel extends DefaultTableModel
{
    @Override
    public void setRowCount(int rowCount)
    {
        if (rowCount < getRowCount())
        {
            for (int i = getRowCount()-1; i >= rowCount; i--)
            {
                ((CustomPanel)getValueAt(i, 0)).removeListeners();
            }
        }
        super.setRowCount(rowCount);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文