Should i use SwingUtilities.invokeLater() inside of SwingWorker.doInBackground()?

别说谁变了你拦得住时间么 提交于 2019-12-04 08:06:17

You can, but the SwingWorker has methods designed to report progress of a background task: you call publish() from doInBackground() to publish progress, and you override process() (which is called in the EDT) in order to display the progress. So the above code could be rewritten as:

public Void doInBackground() {
    for(Object o : objects) {
        doSomething();
        publish("Done for " + o);                           
    }                                             
}

@Override
protected void process(List<String> messages) {
    for (String message : messages) {
        MyGlobalGUIConsole.addMessage(message);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!