SwingWorker
SwingWorker is a utility class developed by Sun Microsystems for the Swing library of the Java programming language. SwingWorker enables proper use of the event dispatching thread. As of Java 6, SwingWorker is included in the JRE.[1] 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.0The event dispatching thread problemSwingWorker 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() {
@Override
public void actionPerformed(ActionEvent e) {
doc = loadXML();
}
});
This will work, but unfortunately, the SwingWorker solutionThis problem is not specific to Java, but common to many GUI models. Creating the workerThe following code defines the SwingWorker, which encapsulates the SwingWorker worker = new SwingWorker<Document, Void>() {
@Override
public Document doInBackground() {
Document intDoc = loadXML();
return intDoc;
}
};
Worker executionExecution is started by using the
Retrieving the resultThe result can be retrieved by using the As calling
private Document doc;
...
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 ex) {
ex.printStackTrace();
} catch (ExecutionException ex) {
ex.printStackTrace();
}
}
}
Complete Worker exampleprivate 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 ex) {
ex.printStackTrace();
} catch (ExecutionException ex) {
ex.printStackTrace();
}
}
};
worker.execute();
}
});
History: Usage before Java 6.0SwingWorker 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.[2] The most recent of these versions dates from 2003 and is often referred to as SwingWorker version 3.[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: SwingWorker worker = new SwingWorker() {
public Object construct() {
... //add the code for the background thread
}
public void finished() {
... //code that you add here will run in the UI thread
}
};
worker.start(); //Start the background thread
The Backport of the Java 6 SwingWorkerA backport of the Java 6 SwingWorker to Java 5 is available at http://swingworker.java.net/[permanent dead link]. Apart from the package name ( Equivalents
References
External links
|
Portal di Ensiklopedia Dunia