【发布时间】:2013-07-16 09:28:17
【问题描述】:
我经常实现一些面板,它们提供控件等常用功能。为此,我希望能够添加侦听器,以便调用者可以附加到控件并获取有关更改的通知。
到目前为止,我只是使用我自己的List 来保存监听器,当我想触发一个操作时,我会遍历列表并调用监听器。从外面看,这基本上看起来像任何其他 Swing 控件,但我想知道这是否真的是应该使用的方法。
我特别想知道Swing 本身是否也在循环中调用侦听器,或者是否有某种队列可以放置操作,以便Swing 决定何时交付此类操作。
当我对此进行调查时,我发现了这段代码:
protected void fireActionPerformed(ActionEvent event)
{
Object[] listeners = listenerList.getListenerList();
ActionEvent e = null;
// 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] instanceof ActionListener)
{
// Lazily create the event:
if (e == null)
{
String actionCommand = event.getActionCommand();
e = new ActionEvent(this,
ActionEvent.ACTION_PERFORMED,
actionCommand,
event.getWhen(),
event.getModifiers());
e.setSource(event.getSource());
}
((ActionListener)listeners[i+1]).actionPerformed(e);
}
}
}
直接访问JComponent中的成员listenerList,感觉有点奇怪。但是到目前为止,我并没有真正找到更好的方法。此外,在为此添加新侦听器时,我现在如下所示执行此操作,但我不确定这是否真的是合适的方式:
public void addQueryListener(ActionListener oListener)
{
listenerList.add(ActionListener.class, oListener);
}
public void removeQueryListener(ActionListener oListener)
{
listenerList.remove(ActionListener.class, oListener);
}
所以我想知道,访问listenerList 成员是否是添加和删除侦听器的正确方法,以便它们的行为与任何其他标准控件一样?还是有一些best practice 应该怎么做,到目前为止我还没有找到?
【问题讨论】:
标签: java swing listener actionlistener jcomponent