Search code examples
javaswinglistener

Howto remove Listeners on SWING JComponents


is there an easy way to remove all Listeners from a 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 amount of Listeners (random types). Since the widget should be removed from the visible part (and won't be needed again) it should be destroyed (and the Listeners should be deleted).

Thanks in advance Joan


Solution

  • There's a workaround mentioned here: