SwingWorker is a popular
utility class
As a topic of economics, utility is used to model worth or value. Its usage has evolved significantly over time. The term was introduced initially as a measure of pleasure or happiness as part of the theory of utilitarianism by moral philosophe ...
developed by
Sun Microsystems
Sun Microsystems, Inc. (Sun for short) was an American technology company that sold computers, computer components, software, and information technology services and created the Java programming language, the Solaris operating system, ZFS, the ...
for the
Swing library of the
Java programming language
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
. SwingWorker enables proper use of the
event dispatching thread The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue. It is an example of the generic concept of event-driven programming, that is popu ...
. As of
Java 6, SwingWorker is included in the
JRE.
Several incompatible, unofficial, versions of SwingWorker were produced from 1998 to 2006, and care must be taken to avoid the abundant documentation on these versions predating Java 6.
Usage in Java 6.0
The event dispatching thread problem
SwingWorker is useful when a time-consuming task has to be performed following a user-interaction event (for example, parsing a huge XML File, on pressing a JButton). The most straightforward way to do it is :
private Document doc;
...
JButton button = new JButton("Open XML");
button.addActionListener(new ActionListener() );
This will work, but unfortunately, the
loadXML()
method will be called in the same
thread as the main Swing thread (the
Event dispatching thread The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue. It is an example of the generic concept of event-driven programming, that is popu ...
), so if the method needs time to perform, the
GUI
The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inste ...
will freeze during this time.
SwingWorker solution
This problem is not specific to Java, but common to many
GUI
The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inste ...
models.
SwingWorker
proposes a way to solve it by performing the time-consuming task on another background thread, keeping the
GUI
The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inste ...
responsive during this time.
Creating the worker
The following code defines the SwingWorker, which encapsulates the
loadXML()
method call :
SwingWorker worker = new SwingWorker() ;
Worker execution
Execution is started by using the
method.
Retrieving the result
The result can be retrieved by using the method.
As calling on the Event Dispatch Thread blocks all events, including repaints, from being processed until the task completes, one must avoid calling it before the lengthy operation has finished. There are two ways to retrieve the result after the task completion :
* override the method. This method is called on the main
event dispatching thread The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue. It is an example of the generic concept of event-driven programming, that is popu ...
.
private Document doc;
...
SwingWorker worker = new SwingWorker()
* register a listener by using the worker method. The listener will be notified of changes in the worker state.
Complete Worker example
private Document doc;
...
JButton button = new JButton("Open XML");
button.addActionListener(new ActionListener() );
History: Usage before Java 6.0
SwingWorker has been part of Java SE only since Java 6.0. Sun has released versions to be used with earlier JDKs, although they were unofficial versions which were not part of the Java SE and were not mentioned in the standard library documentation.
[See the ]javax.swing
package-summar
before
an
J2SE 6.0. Also notice that there is no SwingWorker class listed in the index pages of the documentation, until version 6.0
The most recent of these versions dates from 2003 and is often referred to as SwingWorker version 3.
[You can download it ] Unfortunately, the JDK 6.0 SwingWorker and the Version 3 SwingWorker use different method names and are not compatible. The backport version (see below) is now recommended for pre-Java 6 usage.
An example for instantiating SwingWorker 3 is shown below:
SwingWorker worker = new SwingWorker() ;
worker.start(); //Start the background thread
The
start()
method executes the code added in the construct() method in a separate thread.
To be alerted when the background thread finishes, one need only override the
finished()
method. The
construct()
method can return a result which can later be retrieved using SwingWorker's
get()
method.
Backport of the Java 6 SwingWorker
A backport of the Java 6 SwingWorker to Java 5 is available at http://swingworker.java.net/. Apart from the package name (
org.jdesktop.swingworker
), it is compatible with the Java 6 SwingWorker.
Equivalents
*
System.ComponentModel.BackgroundWorker
-
.NET Framework
The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
*
flash.system.Worker
-
Adobe Flash
Adobe Flash (formerly Macromedia Flash and FutureSplash) is a multimedia Computing platform, software platform used for production of Flash animation, animations, rich web applications, application software, desktop applications, mobile apps, mo ...
*
android.os.AsyncTask
-
Android
References
External links
SwingWorkerclass documentation for Java 7.
from Oracle's Jav
tutorial.
by John O'Conner, January 2007.
{{DEFAULTSORT:Swingworker
Java platform
Articles with example Java code