SwingWorker : Java Glossary

go to home page S words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish 2008-06-18 by Roedy Green ©1996-2008 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
SwingWorker
SwingWorker makes it easy to set up a background Thread that interacts with the Swing GUI. If the background task does not use the GUI components, it is simpler to use an ordinary Thread instead. SwingWorker formally became part of Swing with JDK 1.6, but prior to that, you could find the source and incorporate it.

The idea of SwingWorker is to make it easier to follow the two primary rules of Swing Threads.

There are two fundamental things to understand about Swing and threads:
  1. Time-consuming tasks should not be run on the EDT (aka Event Dispatch Thread aka Swing thread). Otherwise the application becomes unresponsive. The EDT is busy executing your task and hence can’t process GUI events.
  2. Swing components should be accessed on the EDT (aka Event Dispatch Thread aka Swing thread) only. To access components from other threads, you must use SwingUtilities. invokeAndWait, SwingUtilities. invokeLater or SwingWorker.

The magic under the hood is basically that SwingWorker calls your SwingWorker.process method periodically on the EDT thread, so SwingWorker. process does not have to use SwingUtilities. invokeAndWait or SwingUtilities. invokeLater. Your SwingWorker. doInBackground method runs on a non-EDT thread. It communicates with SwingWorker. process which runs on the EDT thread by calling SwingWorker. publish with packets of data for SwingWorker. process.

You might imagine coding with SwingWorker uses fewer threads than SwingUtilities. invokeLater. However invokeLater uses the Runnable interface, without ever generating any extra threads, just the generation of event, enqueing it, and eventually calling the run method. So unless you are using most of the bells and whistles of SwingWorker, you will probably find it simpler just to spin off a garden variety background Thread and use invokeLater.

Like Threads, SwingWorkers objects are not reusable. You must create a new one before you call execute to kick it off.

Learning More

Sun’s Javadoc on the SwingWorker class : available:

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.61] The information on this page is for non-military use only.
You are visitor number 11. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/swingworker.html J:\mindprod\jgloss\swingworker.html