public class EventListenerList extends Object implements Serializable
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); } } }
Modifier and Type | Field and Description |
---|---|
protected Object[] |
listenerList
An array with all currently registered listeners.
|
Constructor and Description |
---|
EventListenerList()
EventListenerList constructor
|
Modifier and Type | Method and Description |
---|---|
<T extends EventListener> |
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> |
getListeners(Class<T> c)
Retrieves the currently subscribed listeners of a particular
type.
|
<T extends EventListener> |
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.
|
protected transient Object[] listenerList
i
, listenerList[i]
indicates
the registered class, and listenerList[i + 1]
is the
listener.public EventListenerList()
public <T extends EventListener> void add(Class<T> t, T listener)
t
- the type of the listener.listener
- the listener to add, which must be an instance of
t
, or of a subclass of t
.IllegalArgumentException
- if listener
is not
an instance of t
(or a subclass thereof).NullPointerException
- if t
is null
.public int getListenerCount()
public int getListenerCount(Class<?> t)
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.public Object[] getListenerList()
public <T extends EventListener> T[] getListeners(Class<T> c)
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.
c
- the class which was passed to add(java.lang.Class<T>, T)
.c
whose elements are the
currently subscribed listeners of the specified type. If there
are no such listeners, an empty array is returned.ClassCastException
- if c
does not implement
the EventListener
interface.NullPointerException
- if c
is
null
.public <T extends EventListener> void remove(Class<T> t, T listener)
t
- the type of the listener.listener
- the listener to remove, which must be an instance
of t
, or of a subclass of t
.IllegalArgumentException
- if listener
is not
an instance of t
(or a subclass thereof).NullPointerException
- if t
is null
.public String toString()
toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)