Customize SwingWorker to improve Swing GUIs
Discover techniques and best practices for robust user interface development
By Yexin Chen, JavaWorld.com, 06/06/03
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Since its inception in JDK 1.2, the maturing Swing toolkit has become an attractive alternative to thin-client presentation
technologies such as JavaServer Pages (JSP), JavaScript, and HTML. Swing offers the benefits of portability, code reusability,
and ease of maintenance, especially when the solution requires a rich user interface (UI) that is deployable in a controlled
environment. For instance, Swing developers need not worry about browser compatibility issues; they can instead devote more
time to improving component functionality and application usability.
Despite these favorable attributes, the Swing toolkit has weaknesses. Swing suffers from the inherent limitation of a larger
resource footprint and dependencies on the Java runtime environment, thereby complicating its deployment process. The more
important issue is that developers should be aware of the UI design goals and Swing's inner mechanics during a project's early
phases. Doing so avoids costly redesigns in later development stages. It is good practice to keep in mind the following design
criteria when putting together the application user interface:
- Screen liveliness: The user interface should maximize its responsiveness to user interaction, especially when the application executes other
processing-intensive tasks.
- Presentation integrity: The user interaction with the application should always obtain consistent and predictable presentation results, unless nondeterminism
is part of the application logic.
- Operation feedback: The user interface should accurately inform users of the current application status through visual feedback. Some examples
of basic feedback mechanisms are the progress meter and the mouse cursor change.
A great deal of effort may be necessary to factor out the complexities required to achieve these design requirements. This
article eases that effort by illustrating best practices through a demo application.
Note: You can download this article's demo from Resources.
Basic Swing concepts review
Let's review the Swing event-dispatch concept, which is the foundation for other topics discussed later. First, when the user
interacts with Swing components, whether it is clicking on a button or resizing a window, the Swing toolkit generates event
objects that contain relevant event information, such as event source and event ID. The event objects are then placed onto
a single event queue ordered by their entry time. While that happens, a separate thread, called the event-dispatch thread,
regularly checks the event queue's state. As long as the event queue is not empty, the event-dispatch thread takes event objects
from the queue one by one and sends them to the interested parties. Finally, the interested parties react to the event notification
by processing logic such as event handling or component painting. Figure 1 illustrates how this works.

Figure 1. Swing event-dispatch model
Since the event-dispatch thread executes all event-processing logic sequentially, it avoids undesirable situations such as
painting a component whose model state is partially updated. This is great news for Swing developers because they can assume
that only one thread, the event-dispatch thread, will process the event-handling code. If two event-dispatch threads worked
on the event queue, Swing developers would need to write additional code for thread safety. Similarly, executing user interface-related
code in any thread other than the event-dispatch thread can lead to unexpected behaviors. If it is absolutely necessary to
execute UI-related code from a separate thread, the developer should use the following code as a workaround:
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download and execute the jar file that contains the source and class files. This demo application is tested under JDK 1.3.1
http://www.javaworld.com/javaworld/jw-06-2003/swingworker/jw-0606-swingworker.jar
- For additional information on UI design goals mentioned in this article, visit Java Look and Feel Design GuidelinesAdvanced Topics
http://java.sun.com/products/jlf/at/book/Responsiveness.html
- Read the Swing Connection articles for more information on the SwingWorker class
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
- The timestamp-based mechanism is covered more in detail as the Asynchronous Completion Token Design Pattern in Pattern-Oriented Software ArchitecturePatterns for Concurrent and Networked Objects by Douglas Schmidt et al. (Wiley & Sons, 2000; ISBN 0471606952)
http://www.cs.wustl.edu/~schmidt/POSA/
- Learn more about multithreading constructs such as latch and future in Doug Lee's Concurrent Programming in JavaDesign Principles and Patterns (Addison-Wesley, 1999; ISBN0201310090)
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html
- The famous Gang of Four book, Design Patterns, Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison-Wesley, 1995; ISBN0201633612)
http://www.amazon.com/exec/obidos/ASIN/0201633612/javaworld
- Read David Geary's Java Design Patterns columns
http://www.javaworld.com/columns/jw-java-design-patterns-index.shtml
- For an article about developing a Swing-based user interface with numerous dialogs, see "Speed Up Your Swing GUI Construction
with Better Building Blocks," David Fraser and Michael Harris (JavaWorld, October 2002)
http://www.javaworld.com/javaworld/jw-10-2002/jw-1004-dialog.html
- Visit the Design Patterns section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-patterns-index.shtml
- Browse the AWT/Swing section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-awt-index.shtml
- Browse the User Interface Design section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-ui-index.shtml
- Sign up for JavaWorld's free weekly email newsletters
http://www.javaworld.com/subscribe