典型的Swing/AWT Event Dispatch Thread长时间占用问题。 处理事件的方法在没有返回以前,AWT的EventQueue所有后续事件,包括PAINT事件是被阻塞的,因此你的界面是不能刷新的,虽然你已经更新了树的model。 在Swing/AWT包括SWT编程中要注意,对于长时间大工作量任务的处理要注意两个问题: * Time-consuming tasks should not be run on the Event Dispatch Thread. Otherwise the application becomes unresponsive. 消耗事件的任务不要放在Event Dispatch Thread上运行。否则界面就会僵死。 * Swing components should be accessed on the Event Dispatch Thread only. Swing组件的访问只能在Event Dispatch Thread来完成。 在JDK 5和JDK 6中引入了一个新的类叫SwingWorker,是专门解决这种问题。详细的内容可以参见java API帮助: http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/zh_CN/api/javax/swing/SwingWorker.html
处理事件的方法在没有返回以前,AWT的EventQueue所有后续事件,包括PAINT事件是被阻塞的,因此你的界面是不能刷新的,虽然你已经更新了树的model。
在Swing/AWT包括SWT编程中要注意,对于长时间大工作量任务的处理要注意两个问题:
* Time-consuming tasks should not be run on the Event Dispatch Thread. Otherwise the application becomes unresponsive.
消耗事件的任务不要放在Event Dispatch Thread上运行。否则界面就会僵死。
* Swing components should be accessed on the Event Dispatch Thread only.
Swing组件的访问只能在Event Dispatch Thread来完成。
在JDK 5和JDK 6中引入了一个新的类叫SwingWorker,是专门解决这种问题。详细的内容可以参见java API帮助:
http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/zh_CN/api/javax/swing/SwingWorker.html
//这个方法是悬浮菜单的事件处理方法
private void actionPerform(ActionEvent e){
MySwingWorker msw=new MySwingWorker(yourTree);
msw.execute();
}
//下面是MySwingWorker的实现:
public class MySwingWorker extends SwingWorker{
private JTree tree;
public MySwingWorker(JTree tree) {
this.tree=tree;
}
protected Object doInBackground() throws Exception {
for(int i=0;i<100;i++){
//生成一个节点并添加到树上....
//...;
//通知EDT线程刷新树
publish();
}
return null;
}
protected void process(List<Object> chunks) {
tree.repaint();
}
}