On this docu about the WeakListeners
I found that code snipped:
private void registerTo(Source source) {
listener = new Listener();
source.addChangeListener(WeakListeners.change (listener, source));
}
private class Listener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
doSomething();
}
What is that source object, they are writing about? We could not find anything on google.
I checked the docs you linked. Further down it states:
It is itself strongly referenced from the implementation of the source (e.g. from its EventListenerList)
Giving you an example of class that can be used there. Indeed in the docs of
WeakListeners.change (listener, source)
source is an Object so it can be whatever you are using as an event source. If you keep reading the doc then you'll see that:
It tries to unregister itself from the source. This is why it needs the reference to the source for the registration. The unregistration is done using reflection, usually looking up the method remove of the source and calling it.
And
This may fail if the source don't have the expected remove* method and/or if you provide wrong reference to source. In that case the weak listener instance will stay in memory and registered by the source, while the listener and observer will be freed.
So I guess you will need to use an interface with a remove method of some sort.
I went through Javadoc index and found this (MenuBar) as an example of what I think you are looking for.
Hope this helps.
Cheers!