Module javafx.base

Class WeakMapChangeListener<K,​V>

java.lang.Object
javafx.collections.WeakMapChangeListener<K,​V>
Type Parameters:
K - the key element type
V - the value element type
All Implemented Interfaces:
WeakListener, MapChangeListener<K,​V>

public final class WeakMapChangeListener<K,​V>
extends Object
implements MapChangeListener<K,​V>, WeakListener
A WeakMapChangeListener can be used, if an ObservableMap should only maintain a weak reference to the listener. This helps to avoid memory leaks, that can occur if observers are not unregistered from observed objects after use.

WeakMapChangeListener are created by passing in the original MapChangeListener. The WeakMapChangeListener should then be registered to listen for changes of the observed object.

Note: You have to keep a reference to the MapChangeListener, that was passed in as long as it is in use, otherwise it will be garbage collected to soon.

Since:
JavaFX 2.1
See Also:
MapChangeListener, ObservableMap, WeakListener
  • Constructor Details

    • WeakMapChangeListener

      public WeakMapChangeListener​(MapChangeListener<K,​V> listener)
      The constructor of WeakMapChangeListener.
      Parameters:
      listener - The original listener that should be notified
  • Method Details

    • wasGarbageCollected

      public boolean wasGarbageCollected()
      Returns true if the linked listener was garbage-collected. In this case, the listener can be removed from the observable.
      Specified by:
      wasGarbageCollected in interface WeakListener
      Returns:
      true if the linked listener was garbage-collected.
    • onChanged

      public void onChanged​(MapChangeListener.Change<? extends K,​? extends V> change)
      Called after a change has been made to an ObservableMap. This method is called on every elementary change (put/remove) once. This means, complex changes like keySet().removeAll(Collection) or clear() may result in more than one call of onChanged method.
      Specified by:
      onChanged in interface MapChangeListener<K,​V>
      Parameters:
      change - the change that was made