SwingWorker
Encyclopedia
SwingWorker is a popular utility class
developed by Sun Microsystems
for the Swing
library of the Java programming language
. enables proper use of the event dispatching thread
. As of Java 6, 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.
This will work, but unfortunately, the
as the main Swing thread (the Event dispatching thread
), so if the method needs time to perform, the GUI
will freeze during this time.
models.
responsive during this time.
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 :
An example for instantiating SwingWorker 3 is shown below:
The
To be alerted when the background thread finishes, one only needs to override the
Utility class
In computer programming, a utility class is a class that defines a set of methods that perform common, often re-used functions. Most utility classes define these common methods under static scope...
developed by Sun Microsystems
Sun Microsystems
Sun Microsystems, Inc. was a company that sold :computers, computer components, :computer software, and :information technology services. Sun was founded on February 24, 1982...
for the Swing
Swing (Java)
Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes — an API for providing a graphical user interface for Java programs....
library of the Java programming language
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
. enables proper use of the event dispatching thread
Event dispatching thread
The event dispatching thread is a background thread used in Java to process events from the Abstract Window Toolkit graphical user interface event queue. These events are primarily update events that cause user interface components to redraw themselves, or input events from input devices such as...
. As of Java 6, 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.
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 :This will work, but unfortunately, the
loadXML
method will be called in the same threadThread (computer science)
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...
as the main Swing thread (the Event dispatching thread
Event dispatching thread
The event dispatching thread is a background thread used in Java to process events from the Abstract Window Toolkit graphical user interface event queue. These events are primarily update events that cause user interface components to redraw themselves, or input events from input devices such as...
), so if the method needs time to perform, the GUI
Graphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...
will freeze during this time.
SwingWorker solution
This problem is not specific to Java, but common to many GUIGraphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...
models.
SwingWorker
proposes a way to solve it by performing the time-consuming task on another background thread, keeping the GUIGraphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...
responsive during this time.
Creating the worker
The following code defines the SwingWorker, which encapsulates theloadXML
method call :Worker execution
Execution is started by using themethod.
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 threadEvent dispatching threadThe event dispatching thread is a background thread used in Java to process events from the Abstract Window Toolkit graphical user interface event queue. These events are primarily update events that cause user interface components to redraw themselves, or input events from input devices such as...
.
- register a listener by using the worker method. The listener will be notified of changes in the worker state.
Complete Worker example
Usage before Java 6.0
SwingWorker has only been part of JSE 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 J2SE and were not mentioned in the standard library documentation. The most recent of these versions dates from 2003 and is often referred to as SwingWorker version 3. 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:
The
start
method executes the code added in the construct method in a separate thread.To be alerted when the background thread finishes, one only needs to 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 has been available at https://swingworker.dev.java.net/ since March 2007. Apart from the package name (org.jdesktop.swingworker
), it is compatible with the Java 6 SwingWorker.See also
- SwingLabsSwingLabsswingLabs is a Sun open source project proposing extensions to the Java Swing GUI toolkit. Available components include:* Sorting, filtering, highlighting for tables, trees, and lists* Find/search* Auto-completion* Login/authentication framework...
- BackgroundWorker, the equivalent .NET Framework.NET FrameworkThe .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
class
External links
- SwingWorker Javadoc from Java 6.
- Worker Threads and SwingWorker from Sun's Java 6 Concurrency in Swing tutorial.
- Improve Application Performance With SwingWorker in Java SE 6 by John O'Conner, January 2007.
- https://swingworker.dev.java.net/ the official page of the developer team.
- Using a Swing Worker Thread New Ways to Perform Background Tasks, from Sun.