java多线程swing,Java Swing多线程和ui冻结

Can't figure this one out. Using worker or invokeLater, the UI still freeze. After each file is downloaded, I want a JList to be updated. But the JList will only update after the tread returns.

Here is the code:

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

...//download action...

//for my jList1 to be updated after each file.

MyUi.updateJlist(fileName[i]);

}

}

}

Any example would help.

解决方案

Download the file on a background thread and wrap just updateJlist() in a Runnable.

SwingWorker would be more reliable.

Addendum: As @mre notes, SwingWorker also makes it easy to report interim results, as shown here.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值