Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwingWorker get a result

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?

like image 604
Dmitry Belaventsev Avatar asked Jul 20 '26 00:07

Dmitry Belaventsev


2 Answers

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.

like image 168
Paul Avatar answered Jul 22 '26 14:07

Paul


You can override done() and/or process(), as shown here and here.

like image 45
trashgod Avatar answered Jul 22 '26 14:07

trashgod