I have a SwingWorker, which does some computations in the background (these actions are situated in the overridden doInBackground()
method). So, I also use the execute()
method to begin the computations. How I can get the result of these computations when they are finished?
Override the done
method - that method will be called when the work is complete. Oracle has a comprehensive tutorial here: Improve Application Performance With SwingWorker in Java SE 6
Also see another SO question: How do I use SwingWorker in Java?
You can call get
to retrieve the results but if the worker isn't done the thread will block until the worker is done. That means if you call get
from the Event Dispatch Thread (EDT) your GUI will be unresponsive if the worker isn't done. You can call isDone
to determine if the worker has completed.
Finally, you can attach a property change listener to be notified of the worker's progress, including when it completes its task. The first link I posted gives an example of that.