|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjavax.swing.event.EventListenerList
public class EventListenerList
保存 EventListener 列表的类。单个实例可用来保存使用列表的实例的所有侦听器(所有类型)。该类的责任是使用 EventListenerList 提供类型安全的 API(最好遵守 JavaBeans 规范)和方法,这些方法将事件通知方法指派给列表上适当的 Event Listener。 此类的主要优点是,在没有侦听器的情况下,它相对廉价一些,它在单个地方为事件侦听器提供序列化,同时还提供某种程度的 MT 安全(在正确使用时)。 用例:假定正在定义发送 FooEvent 的类,并希望允许该类的用户注册 FooListener 并在发生 FooEvent 时接收通知。则应该将以下代码添加到类定义中:
EventListenerList listenerList = new EventListenerList();
FooEvent fooEvent = null;
public void addFooListener(FooListener l) {
listenerList.add(FooListener.class, l);
}
public void removeFooListener(FooListener l) {
listenerList.remove(FooListener.class, l);
}
// Notify all listeners that have registered interest for
// notification on this event type. The event instance
// is lazily created using the parameters passed into
// the fire method.
protected void fireFooXXX() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==FooListener.class) {
// Lazily create the event:
if (fooEvent == null)
fooEvent = new FooEvent(this);
((FooListener)listeners[i+1]).fooXXX(fooEvent);
}
}
}
应该将 foo 更改为适当的名称,并将 fireFooXxx 也更改为适当的方法名称。FooListener 接口中的每个通知方法都应该有一个 fire 方法。
警告:此类的序列化对象将与以后的 Swing 版本不兼容。当前的序列化支持适用于短期存储或运行相同 Swing 版本的应用程序之间的 RMI。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder。
| 字段摘要 | |
|---|---|
protected Object[] |
listenerList
|
| 构造方法摘要 | |
|---|---|
EventListenerList()
|
|
| 方法摘要 | ||
|---|---|---|
|
add(Class<T> t,
T l)
将侦听器作为指定类型的侦听器进行添加。 |
|
int |
getListenerCount()
返回此侦听器列表中侦听器的总数。 |
|
int |
getListenerCount(Class<?> t)
返回此侦听器列表中所提供类型的侦听器的总数。 |
|
Object[] |
getListenerList()
将事件侦听器列表以 ListenerType 侦听器对数组的形式传回。 |
|
|
getListeners(Class<T> t)
返回给定类型的所有侦听器组成的数组。 |
|
|
remove(Class<T> t,
T l)
将侦听器作为指定类型的侦听器进行移除。 |
|
String |
toString()
返回此 EventListenerList 的字符串表示形式。 |
|
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 字段详细信息 |
|---|
protected transient Object[] listenerList
| 构造方法详细信息 |
|---|
public EventListenerList()
| 方法详细信息 |
|---|
public Object[] getListenerList()
public <T extends EventListener> T[] getListeners(Class<T> t)
ClassCastException - 如果所提供的类不能分配给 EventListenerpublic int getListenerCount()
public int getListenerCount(Class<?> t)
public <T extends EventListener> void add(Class<T> t,
T l)
t - 要添加的侦听器的类型l - 要添加的侦听器
public <T extends EventListener> void remove(Class<T> t,
T l)
t - 要移除的侦听器的类型l - 要移除的侦听器public String toString()
Object 中的 toString
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。