How to remove listeners on SWING JComponents

Is there an easy way to remove all listeners from JComponent?

JComponent widget = getComponentOverScaryMethod(); EventListener[] listners = widget.getListeners(EventListener.class); for (EventListener l : listners) { widget.remove*RandomListener*(l); } 

Background:

I have a JComponent with an unknown number of Listeners (random types). Since the widget must be removed from the visible part (and no longer needed), it must be destroyed (and Listeners must be removed).

Thanks in advance Joan

+8
java listener swing
source share
2 answers
+4
source share

if you remove the widget from the parent, it should never fire for events again, and the listeners should be automatically released by gc

the only reason that listeners are not exempted is because of a leak that makes the widget accessible, even if it should not be

+1
source share

All Articles