javax.swing.event
Class EventListenerList
- Serializable
A utility class for keeping track of
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);
}
}
}
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
listenerList
protected 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.
extends EventListener> T[] getListeners
public extends EventListener> T[] getListeners(Class 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.
c
- the class which was passed to EventListenerList
.
- 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.
extends EventListener> void add
public extends EventListener> void add(Class t,
T listener)
Registers a listener of a specific type.
t
- the type of the listener.listener
- the listener to add, which must be an instance of
t
, or of a subclass of t
.
extends EventListener> void remove
public extends EventListener> void remove(Class t,
T listener)
Removes a listener of a specific type.
t
- the type of the listener.listener
- the listener to remove, which must be an instance
of t
, or of a subclass of t
.
getListenerCount
public int getListenerCount()
Determines the number of listeners.
getListenerCount
public int getListenerCount(Class> t)
Determines the number of listeners of a particular class.
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.
- An array containing the listener types and references.
toString
public String toString()
Returns a string representation of this object that may be useful
for debugging purposes.
- toString in interface Object
EventListenerList.java --
Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.