无法弄清楚这一点。使用worker或invokeLater,UI仍然冻结。在每个文件下载后,我想要更新JList。但JList只会在胎面返回后更新。Java Swing多线程和用户界面冻结
下面是代码:
public class MyUi extends javax.swing.JFrame{
...
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
SwingUtilities.invokeLater(new Runnable() {
//To get out of the event tread
public void run() {
dl();
}
});
}
private void dl(){
...
//ini and run the download class
Download myDownload = new Download();
myDownload.doDownload(myDlList);
}
public void updateJlist(String myString){
myModel.addElement(myString);
jList1.repaint();
}
}
public class Download{
...
public void doDownload(String[] fileName){
for(int i=0; i<fileName.length; i++){
...//download action...
//for my jList1 to be updated after each file.
MyUi.updateJlist(fileName[i]);
}
}
}
任何例子会有所帮助。
+1的SwingWorker与中期业绩。 – mre
在后台运行下载工作正常,我不需要换行updateJlist()。对于SwingWorker,我将需要阅读更多内容并执行测试。我不完全理解它。谢谢! – marcb
我很高兴它正在工作,但我仍然[很警惕](http://stackoverflow.com/a/7158505/230513)从非EDT线程更新GUI。如果你需要'SwingWorker'的帮助,请不要犹豫,发布一个问题。 – trashgod