Skip to content
Advertisement

Java GUI – Progress bar doesn’t update until the async task is finished

I am using CompletableFuture to run a long running operation. Meanwhile, i use SwingWorker to update a progress bar with increments of 5.

JavaScript

The progress bar doesn’t update until the asynchronous method is finished. I have also tried doing this operation on the EDT thread, but to no avail. What you are seeing is basically me trying to just do trial and error at this point.

Why is the progress bar not updating? How can I fix this?

Advertisement

Answer

Stop and take a closer look at Worker Threads and SwingWorker.

A SwingWorker is meant to allow you to perform a long running task, which may produce intermediate results, and allow you to publish those results back to the Event Dispatching Thread to be safely processed (via the process method).

You “could” publish the progress updates and update the progress bar in process method, but SwingWorker already provides a progress property, which you can monitor. Take a look at the SwingWorker JavaDocs for a number of ready made examples!

Run Example

Simple Example

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement