JIDE 3.5.15

com.jidesoft.swing
Class WeakPropertyChangeListener

java.lang.Object
  extended by com.jidesoft.swing.WeakPropertyChangeListener
All Implemented Interfaces:
PropertyChangeListener, EventListener

public class WeakPropertyChangeListener
extends Object
implements PropertyChangeListener

How to use this:

 KeyboardFocusManager focusManager =
 KeyboardFocusManager.getCurrentKeyboardFocusManager();
 

// instead of registering directly use weak listener // focusManager.addPropertyChangeListener(focusOwnerListener);

focusManager.addPropertyChangeListener( new WeakPropertyChangeListener(focusOwnerListener, focusManager));

How does this work:

Instead of registering propertyChangeListener directly to keyboardFocusManager, we wrap it inside WeakPropertyChangeListener and register this weak listener to keyboardFocusManager. This weak listener acts a delegate. It receives the propertyChangeEvents from keyboardFocusManager and delegates it the wrapped listener.

The interesting part of this weak listener, it hold a weakReference to the original propertyChangeListener. so this delegate is eligible for garbage collection which it is no longer reachable via references. When it gets garbage collection, the weakReference will be pointing to null. On next propertyChangeEvent notification from keyboardFocusManager, it find that the weakReference is pointing to null, and unregisters itself from keyboardFocusManager. Thus the weak listener will also become eligible for garbage collection in next gc cycle.

This concept is not something new. If you have a habit of looking into swing sources, you will find that AbstractButton actually adds a weak listener to its action. The weak listener class used for this is : javax.swing.AbstractActionPropertyChangeListener; This class is package-private, so you don't find it in javadoc.

The full-fledged, generic implementation of weak listeners is available in Netbeans OpenAPI: WeakListeners.java . It is worth to have a look at it.

Author:
Santhosh Kumar T - santhosh@in.fiorano.com

Constructor Summary
WeakPropertyChangeListener(PropertyChangeListener listener, Object src)
           
 
Method Summary
 void itemStateChanged(ItemEvent e)
           
 void propertyChange(PropertyChangeEvent evt)
           
 void stateChanged(ChangeEvent e)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WeakPropertyChangeListener

public WeakPropertyChangeListener(PropertyChangeListener listener,
                                  Object src)
Method Detail

propertyChange

public void propertyChange(PropertyChangeEvent evt)
Specified by:
propertyChange in interface PropertyChangeListener

itemStateChanged

public void itemStateChanged(ItemEvent e)

stateChanged

public void stateChanged(ChangeEvent e)

JIDE 3.5.15