检查组件何时从 JTable 中删除
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DefaultCellEditor
有一个stopCellEditing()
方法,您应该能够向其中添加代码。或者您可以将 PropertyChangeListener 添加到表中:
The
DefaultCellEditor
has astopCellEditing()
method you should be able to add your code to.Or maybe you can add a PropertyChangeListener to the table:
我通过创建一个新的 TableModel 并重写 setRowCount() 方法来修复它。
I fixed it by creating a new TableModel and overriding the setRowCount() method.