Java - Difference between SwingWorker and SwingUtilities.invokeLater()
- by Yatendra Goel
SwingWorker is used for the following purposes:
For running long-running tasks in a different thread so as to prevent the GUI from being unresponsive
For updating GUI with the results produced by the long-running task at the end of the task through done() method.
For updating GUI from time to time with the intermediate results produced and published by the task with the help of publish() and process() methods.
SwingUtilities.invokeLater() can perform the above tasks as follows:
Instead of executing SwingWorker.execute() method from the EDT, we can execute ExecutorService.submit(new MyRunnable()) as it will also create another thread which can execute long-running task.
For updating GUI at the end of the task, we can put code (written in done() method of case1) SwingUtilites.invokeLater(new RunnableToExecuteDoneMethodCode()) at the end of the task.
For updating GUI in the middle of the task, we can put code (written in process() method of case1) SwingUtilites.invokeLater(new RunnableToExecuteProcessMethodCode()) at the place where we called publish() method in case1.
I am asking this question because the problem specified in question http://stackoverflow.com/questions/2797483/java-swingworker-can-we-call-one-swingworker-from-other-swingworker-instead-o/2824306#2824306 can be solved by SwingUtilities.invokeLater() but can't be solved with SwingWorker