下面是LookupListener接口的定义
package org.openide.util;
import java.util.*;
/** General listener for changes in lookup.
*
* @author Jaroslav Tulach
*/
public interface LookupListener extends EventListener {
/** A change in lookup occured. Please note that this method
* should never block since it might be called from lookup implementation
* internal threads. If you block here you are in risk that the thread
* you wait for might in turn to wait for the lookup internal thread to
* finish its work.
* @param ev event describing the change
*/
public void resultChanged(LookupEvent ev);
}
import java.util.*;
/** General listener for changes in lookup.
*
* @author Jaroslav Tulach
*/
public interface LookupListener extends EventListener {
/** A change in lookup occured. Please note that this method
* should never block since it might be called from lookup implementation
* internal threads. If you block here you are in risk that the thread
* you wait for might in turn to wait for the lookup internal thread to
* finish its work.
* @param ev event describing the change
*/
public void resultChanged(LookupEvent ev);
}
TopComponent实现了LookupListener接口,并注册为result = Utilities.actionsGlobalContext().lookupResult(XXX.class)的监听器
问题:TopComponent覆盖的resultChanged是否在EDT中执行。
1 尝试在resultChanged中输出当前线程名:Thread[AWT-EventQueue-0,6,IDE Main]
2 在这篇教程中http://platform.netbeans.org/tutorials/nbm-selection-1.html,其resultChanged方法中直接调用了修改JLabel对象文本的方法,如下
@Override
public void resultChanged(LookupEvent lookupEvent) {
Lookup.Result r = (Lookup.Result) lookupEvent.getSource();
Collection c = r.allInstances();
if (!c.isEmpty()) {
APIObject o = (APIObject) c.iterator().next();
jLabel1.setText (Integer.toString(o.getIndex()));
jLabel2.setText (o.getDate().toString());
} else {
jLabel1.setText("[no selection]");
jLabel2.setText ("");
}
}
public void resultChanged(LookupEvent lookupEvent) {
Lookup.Result r = (Lookup.Result) lookupEvent.getSource();
Collection c = r.allInstances();
if (!c.isEmpty()) {
APIObject o = (APIObject) c.iterator().next();
jLabel1.setText (Integer.toString(o.getIndex()));
jLabel2.setText (o.getDate().toString());
} else {
jLabel1.setText("[no selection]");
jLabel2.setText ("");
}
}
总结:resultChanged很可能直接在EDT中执行,但尚未找到明确的说明。