Package com.jidesoft.swing
Class WeakPropertyChangeListener
- java.lang.Object
-
- com.jidesoft.swing.WeakPropertyChangeListener
-
- All Implemented Interfaces:
java.beans.PropertyChangeListener
,java.util.EventListener
public class WeakPropertyChangeListener extends java.lang.Object implements java.beans.PropertyChangeListener
How to use this:
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.KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); // instead of registering directly use weak listener // focusManager.addPropertyChangeListener(focusOwnerListener); focusManager.addPropertyChangeListener( new WeakPropertyChangeListener(focusOwnerListener, focusManager));
- Author:
- Santhosh Kumar T - santhosh@in.fiorano.com
-
-
Constructor Summary
Constructors Constructor Description WeakPropertyChangeListener(java.beans.PropertyChangeListener listener, java.lang.Object src)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
itemStateChanged(java.awt.event.ItemEvent e)
void
propertyChange(java.beans.PropertyChangeEvent evt)
void
stateChanged(javax.swing.event.ChangeEvent e)
-
-
-
Method Detail
-
propertyChange
public void propertyChange(java.beans.PropertyChangeEvent evt)
- Specified by:
propertyChange
in interfacejava.beans.PropertyChangeListener
-
itemStateChanged
public void itemStateChanged(java.awt.event.ItemEvent e)
-
stateChanged
public void stateChanged(javax.swing.event.ChangeEvent e)
-
-