Jump to content

SwingWorker

From Alfawiki
Sun Microsystems
IndustryComputer software
FoundedFebruary 24, 1982; 44 years ago (1982-02-24)
FateAcquired by Oracle Corporation in 2010
Headquarters
Santa Clara, California
,
United States
ProductsJava platform, Solaris, SPARC processors

SwingWorker is a utility class created by Sun Microsystems for the Swing framework within the Java programming language. It facilitates correct management of the event dispatching thread and has been part of the JRE starting from Java 6.

Between 1998 and 2006, several unofficial and incompatible SwingWorker versions circulated, making it essential to rely on the official implementation introduced in Java 6 and later.

Challenges with the event dispatching thread

The primary problem SwingWorker solves is handling long-running operations (such as parsing large XML documents) initiated by user interactions. Without it, these tasks would block the main Swing thread, causing the interface to become unresponsive.

How SwingWorker addresses this

By running intensive tasks on a separate background thread, SwingWorker ensures that the GUI remains interactive.

Defining the worker

Here is an example of creating a SwingWorker to asynchronously execute loadXML(): <syntaxhighlight lang="java"> SwingWorker<Document, Void> worker = new SwingWorker<Document, Void>() {

   @Override
   public Document doInBackground() {
       return loadXML();
   }

}; </syntaxhighlight>

Starting the worker

Invoke the Template:Javadoc:SE method to begin processing.

Obtaining results

To access the outcome, use Template:Javadoc:SE. However, calling this method on the Event Dispatch Thread before the task finishes can cause the interface to freeze. Instead, consider one of these two approaches once the background operation completes:

<syntaxhighlight lang="java"> private Document doc; ... SwingWorker<Document, Void> worker = new SwingWorker<Document, Void>() {

   @Override
   public Document doInBackground() {
       return loadXML();
   }
   @Override
   public void done() {
       try {
           doc = get();
       } catch (InterruptedException | ExecutionException ex) {
           ex.printStackTrace();
       }
   }

}; </syntaxhighlight>

Full example of a SwingWorker

<syntaxhighlight lang="java"> private Document doc; ... JButton button = new JButton("Open XML"); button.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
       SwingWorker<Document, Void> worker 
           = new SwingWorker<Document, Void>() {
           @Override
           public Document doInBackground() {
               Document intDoc = loadXML();
               return intDoc;
           }
           @Override
           public void done() {
               try {
                   doc = get();
               } catch (InterruptedException | ExecutionException ex) {
                   ex.printStackTrace();
               }
           }
       };
       worker.execute();
   }

}); </syntaxhighlight>

History: Usage Prior to Java 6.0

SwingWorker was officially incorporated into Java SE starting with version 6.0. Before this, earlier iterations were distributed unofficially for prior JDK releases and were not part of the documented standard library. The most recent of these legacy versions, identified as SwingWorker version 3 from 2003, features different method signatures and is incompatible with the SwingWorker introduced in Java 6.0. For development environments predating Java 6, utilizing the backport is advised.

Sample creation of a SwingWorker 3 instance:

<syntaxhighlight lang="java"> SwingWorker worker = new SwingWorker() {

   public Object construct() {
       ... // code executing in background thread
   }
   public void finished() {
       ... // code running on UI thread after task completion
   } 

}; worker.start(); // Initiates background thread </syntaxhighlight>

The start() method triggers construct() to execute on a separate thread. Overriding finished() enables handling once the background process finishes. The outcome produced by construct() can later be accessed using get().

Backport of Java 6 SwingWorker

A Java 5-compatible backport of the Java 6 SwingWorker implementation is accessible at http://swingworker.java.net/. Apart from the package designation (org.jdesktop.swingworker), it maintains compatibility with the Java 6 version.

Comparable Technologies

External Resources