javax.swing.event
public class EventListenerList extends Object implements Serializable
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> void | add(Class<T> t, T listener)
Registers a listener of a specific type.
|
| int | getListenerCount()
Determines the number of listeners. |
| int | getListenerCount(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> void | remove(Class<T> t, T listener)
Removes a listener of a specific type.
|
| String | toString()
Returns a string representation of this object that may be useful
for debugging purposes. |
i, listenerList[i] indicates
the registered class, and listenerList[i + 1] is the
listener.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.
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.
Returns: An array containing the listener types and references.
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
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.