Swing is single-threaded: all component creation, painting and event handling must run on the Event Dispatch Thread (EDT). To keep a Swing UI responsive, move slow work to a background thread with SwingWorker (or an executor) and hand results back to the EDT with SwingUtilities.invokeLater or SwingWorker.process and done. For periodic UI updates, use javax.swing.Timer.
Blocking the EDT with a Thread.sleep, a network call or a database query freezes repainting and input until the call returns. The sections below show the correct tools and the mistakes to avoid.
SwingWorker
javax.swing.TimerSwing components are not thread-safe. Listeners such as ActionListener run on the EDT, and so does painting. Two rules follow from that:
You can check which thread you are on with SwingUtilities.isEventDispatchThread(). Create the UI itself on the EDT too, using SwingUtilities.invokeLater.
SwingWorkerSwingWorker<T, V> is the standard tool. doInBackground() runs on a worker thread, publish(V...) sends intermediate values, process(List<V>) receives them on the EDT, and done() runs on the EDT after the task ends. T is the final result type and V is the type of intermediate results.
The button handler runs on the EDT and returns immediately. The sleeping happens on a pool thread, and the progress bar and label are only touched in process and done. Calling get() inside done() is safe because the task has already finished, and it rethrows any exception from doInBackground() wrapped in an ExecutionException.
For a plain percentage, call setProgress(int) inside doInBackground() and listen for the progress property. To support a Cancel button, call worker.cancel(true) and check isCancelled() in the loop. Interrupting a Thread.sleep throws InterruptedException, which you should treat as a signal to stop.
Property change events from SwingWorker are delivered on the EDT.
If you use an ExecutorService, a CompletableFuture or a virtual thread instead of SwingWorker, you must switch back to the EDT yourself.
Virtual threads (final in Java 21) are a good fit for blocking I/O such as HTTP or JDBC calls. Note that the try-with-resources block here closes the executor, which waits for submitted tasks, so in a real application keep the executor in a field and shut it down when the window closes instead of closing it on the EDT.
SwingUtilities.invokeLater posts work to the EDT without waiting. invokeAndWait blocks the caller until the work finishes and must never be called from the EDT itself.
javax.swing.Timerjavax.swing.Timer fires ActionEvents on the EDT at a fixed interval, so it is the right choice for clocks, animations and polling that only does quick work.
Because the callback runs on the EDT, do not put slow work in it. Do not confuse it with java.util.Timer, which runs tasks on its own thread and must not touch components.
| Need | Use | Runs UI code on |
|---|---|---|
| One long task with progress and a result | SwingWorker | EDT in process and done |
| Repeating quick UI update | javax.swing.Timer | EDT |
| Many parallel or I/O-heavy tasks | ExecutorService or virtual threads plus invokeLater | EDT via invokeLater |
| Fire-and-forget UI change from another thread | SwingUtilities.invokeLater | EDT |
Thread.sleep or blocking I/O in a listener. The window stops repainting.JLabel, JTable model or JProgressBar from doInBackground().get() on a SwingWorker from the EDT before it has finished. It blocks the UI.InterruptedException. Restore the flag with Thread.currentThread().interrupt() or stop the work.SwingWorker uses a shared, limited pool, so long-blocking workers can starve others; use your own executor for heavy parallelism.It is the single thread that handles all Swing events and painting. All reads and writes of component state should happen on it.
SwingWorker better than a plain Thread?For UI-bound tasks, yes. It gives you publish, process, done, progress properties and cancellation, all delivered on the EDT, which a raw Thread does not.
Wrap the update in SwingUtilities.invokeLater(() -> ...). It queues the code to run on the EDT.
javax.swing.Timer instead of java.util.Timer?Use javax.swing.Timer whenever the callback changes the UI, because it fires on the EDT. java.util.Timer runs on its own thread and is unsafe for components.
Keep the EDT free for painting and events. Run slow work in SwingWorker or an executor, send results back to the EDT with process, done or invokeLater, and use javax.swing.Timer for periodic UI changes. Never touch components from a background thread.