[英]Java Swing multi threads and ui freezes
Can't figure this one out. 无法弄清楚这一点。 Using worker or invokeLater, the UI still freeze.
使用worker或invokeLater,UI仍然冻结。 After each file is downloaded, I want a JList to be updated.
下载每个文件后,我希望更新JList。 But the JList will only update after the tread returns.
但是JList仅在踩踏返回后才更新。
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<fileName.length; i++){
...//download action...
//for my jList1 to be updated after each file.
MyUi.updateJlist(fileName[i]);
}
}
}
Any example would help. 任何示例都会有所帮助。
invokeLater
与您期望的完全相反-它在EDT上运行操作,从而解释了行为。
Download the file on a background thread and wrap just updateJlist()
in a Runnable
. 将文件下载到后台线程上,然后将
updateJlist()
包装到Runnable
。
SwingWorker
would be more reliable. SwingWorker
将更加可靠。
Addendum: As @mre notes, SwingWorker
also makes it easy to report interim results, as shown here . 附录:作为@mre笔记,
SwingWorker
也可以很容易地报告中期业绩,如图所示这里 。
I have create a WorkerThread class which take care of Threads and GUI current/main thread . 我创建了一个WorkerThread类,该类负责线程和GUI当前/主线程。 i have put my GUI application in construct() method of WorkerThread when an event fire to start XXXServer then all threads are activate and GUI work smoothlly wihout freeze.
当发生事件启动XXXServer时,我将我的GUI应用程序放在WorkerThread的Construct()方法中,然后所有线程均被激活,GUI顺利工作,而没有冻结。 have a look.
看一看。
/** * Action Event * * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ / ** *动作事件* * @参见java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)* /
public void actionPerformed(ActionEvent ae) { log.info("actionPerformed begin..." + ae.getActionCommand()); 公共无效actionPerformed(ActionEvent ae){log.info(“ actionPerformed begin ...” + ae.getActionCommand());
try {
if (ae.getActionCommand().equals(btnStart.getText())) {
final int portNumber = 9990;
try {
WorkerThread workerThread = new WorkerThread(){
public Object construct(){
log.info("Initializing the Server GUI...");
// initializing the Server
try {
xxxServer = new XXXServer(portNumber);
xxxServer.start();
btnStart.setEnabled(false);
} catch (IOException e) {
// TODO Auto-generated catch block
log.info("actionPerformed() Start button ERROR IOEXCEPTION..." + e.getMessage());
e.printStackTrace();
}
return null;
}
};workerThread.start();
} catch (Exception e) {
log.info("actionPerformed() Start button ERROR..." + e.getMessage());
e.printStackTrace();
}
} else if (ae.getActionCommand().equals(btnStop.getText())) {
log.info("Exit..." + btnStop.getText());
closeWindow();
}
} catch (Exception e) {
log
.info("Error in ServerGUI actionPerformed==="
+ e.getMessage());
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.