SwingWorker Is Bound to Please
SwingWorker now incorporates new language features and classes that were not previously available. SwingWorker now uses generics, variable argument lists, and enums. It also notifies PropertyChangeListener objects of changes to bound properties andimplements the Future interface from the java.util.concurrent package. Further, it makes extensive use of the java.util.concurrent package.
Generics
SwingWorker uses two generic type variables: TT and V. The formal type parameter T is used for the return type for the SwingWorker object's get method. The formal type parameter V is used for publishing intermediate results. You will benefit by writing code that is more strongly type checked at compile time than if you were not using generics. Your code should also involve less casting and it will be clear to the compiler as well as people reading your code what types are required by your API.
Variable Argument Lists
SwingWorker provides mechanisms which make it easy to communicate the state of your time-consuming code back to the event dispatch thread. Intermediate results can be published using the publish method which accepts a variable argument list of whatever V type variable you are using. Published intermediate results are received on the event dispatch thread by overriding the process method.
Objects that implement PropertyChangeListener can be informed on the event dispatch thread of changes to the SwingWorker object's bound properties. A PropertyChangeListener provides an easy way to hide view details from your time-consuming code. In addition to the built-in "state" and "progress" properties, you are free to implement your own bound properties.
The state property can be anyone of the SwingWorker object's StateValue values: PENDING, STARTED, and DONE. The StateValue enum is easy to use in a PropertyChangeListener by using a switch statement (Listing 2), but notice that the switch statement is not making use of the PENDING state. This is because a SwingWorker is used only once and the initial state is PENDING followed by STARTED, and finally, DONE.
A PropertyChangeListener can also be used to update a progress bar, as in the code below. Within your time-consuming code, simply invoke the setProgress method with an int parameter ranging from 0 to 100:
public void propertyChange(PropertyChangeEvent evt){
if ("progress".equals(evt.getPropertyName())){
jProgressBar.setValue(swingWorker.getProgress());
}
}
You've how to provide asynchronous feedback from time-consuming code using the publish and process methods. You can also "listen" for changes to the built-in state and progress bound properties and define and use custom bound properties. Finally, overriding the done method (see code below) provides the final asynchronous feedback mechanism. Together these mechanisms provide easy, flexible ways to help Swing programmers write responsive applications.
protected void done() {
statusLabel.setText("Done");
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|