javax.swing.event

Class EventListenerList

public class EventListenerList extends Object implements Serializable

A utility class for keeping track of {@link EventListener}s.

Example for using this class:

 import java.util.EventListener;
 import javax.swing.event.EventListenerList;

 class Foo
 {
   protected final EventListenerList listeners = new EventListenerList();
   protected BarClosedEvent barClosedEvent = null;

   public void addBarListener(BarListener l)
   {
     listeners.add(BarListener.class, l);
   }

   public void removeBarListener(BarListener l)
   {
     listeners.remove(BarListener.class, l);
   }

   protected void fireBarClosedEvent()
   {
     Object[] l = listeners.getListenerList();

     for (int i = l.length - 2; i >= 0; i -= 2)
       if (l[i] == BarListener.class)
         {
           // Create the event on demand, when it is needed the first time.
           if (barClosedEvent == null)
             barClosedEvent = new BarClosedEvent(this);

           ((BarClosedListener) l[i + 1]).barClosed(barClosedEvent);
         }
   }
 }
Field Summary
protected Object[]listenerList
An array with all currently registered listeners.
Constructor Summary
EventListenerList()
EventListenerList constructor
Method Summary
<T extends EventListener> voidadd(Class<T> t, T listener)
Registers a listener of a specific type.
intgetListenerCount()
Determines the number of listeners.
intgetListenerCount(Class<?> t)
Determines the number of listeners of a particular class.
Object[]getListenerList()
Returns an array containing a sequence of listenerType/listener pairs, one for each listener.
<T extends EventListener> T[]getListeners(Class<T> c)
Retrieves the currently subscribed listeners of a particular type.
<T extends EventListener> voidremove(Class<T> t, T listener)
Removes a listener of a specific type.
StringtoString()
Returns a string representation of this object that may be useful for debugging purposes.

Field Detail

listenerList

protected transient Object[] listenerList
An array with all currently registered listeners. The array has twice as many elements as there are listeners. For an even integer i, listenerList[i] indicates the registered class, and listenerList[i + 1] is the listener.

Constructor Detail

EventListenerList

public EventListenerList()
EventListenerList constructor

Method Detail

add

public <T extends EventListener> void add(Class<T> t, T listener)
Registers a listener of a specific type.

Parameters: t the type of the listener. listener the listener to add, which must be an instance of t, or of a subclass of t.

Throws: IllegalArgumentException if listener is not an instance of t (or a subclass thereof). NullPointerException if t is null.

getListenerCount

public int getListenerCount()
Determines the number of listeners.

getListenerCount

public int getListenerCount(Class<?> t)
Determines the number of listeners of a particular class.

Parameters: t the type of listeners to be counted. In order to get counted, a subscribed listener must be exactly of class t. Thus, subclasses of t will not be counted.

getListenerList

public Object[] getListenerList()
Returns an array containing a sequence of listenerType/listener pairs, one for each listener.

Returns: An array containing the listener types and references.

getListeners

public <T extends EventListener> T[] getListeners(Class<T> c)
Retrieves the currently subscribed listeners of a particular type. For a listener to be returned, it must have been registered with exactly the type c; subclasses are not considered equal.

The returned array can always be cast to c[]. Since it is a newly allocated copy, the caller may arbitrarily modify the array.

Parameters: c the class which was passed to {@link #add}.

Returns: an array of c whose elements are the currently subscribed listeners of the specified type. If there are no such listeners, an empty array is returned.

Throws: ClassCastException if c does not implement the {@link EventListener} interface. NullPointerException if c is null.

Since: 1.3

remove

public <T extends EventListener> void remove(Class<T> t, T listener)
Removes a listener of a specific type.

Parameters: t the type of the listener. listener the listener to remove, which must be an instance of t, or of a subclass of t.

Throws: IllegalArgumentException if listener is not an instance of t (or a subclass thereof). NullPointerException if t is null.

toString

public String toString()
Returns a string representation of this object that may be useful for debugging purposes.