Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck when component is removed from JTable
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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?</p> <p>How I add rows to the table:</p> <pre><code>public void fillTable() { DefaultTableModel model = (DefaultTableModel) table.getModel(); model.setRowCount(0); CustomPanel panel = new CustomPanel(getSomeObject()); model.addRow(new Object[]{panel}); } </code></pre> <p>How the custom panel registers as a listener:</p> <pre><code>public class CustomPanel extends JPanel implements CustomObjectListener { public CustomPanel(CustomObjet obj) { obj.addListener(this); } @Override public void CustomObjectEvent(Event evt) { handle event; } } </code></pre> <p>How the event is thrown:</p> <pre><code>public class CustomObject { ArrayList&lt;CustomObjectListener&gt; listeners = new ArrayList&lt;CustomObjectListener&gt;(); 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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload