SwingCommand
![]() |
![]() |
What is SwingCommand?
The SwingCommand library is a small set of utility classes based around the Command pattern. It provide facilities for implementing asynchronous commands in Swing.
SwingCommand can be downloaded here
An alternative to SwingWorker
SwingCommand may be considered as an alternative to SwingWorker , a utility which has been available as part of the jdk from 1.6 onwards. Just like SwingWorker, it seeks to make it easier to manage background tasks and handle the various thread programming issues within Swing applications. For a full view of these issues, and threading and concurrency in Swing applications in general, see the Swing Concurrency Tutorial.SwingCommand provides several features in addition to those provided by SwingWorker. The most significant difference is that whereas SwingWorker instances may only be run once, SwingCommand instances can be invoked multiple times. Each execution of a SwingCommand creates a Task. The Task performs the command processing, and holds the parameters and state required for that execution - in fact the Task instance which is created when a SwingCommand executes is roughly analagous to a SwingWorker instance.
Since SwingCommands can be executed multiple times, a command instance can be shared between different classes, and triggered from different places (or by different Actions) within your Swing application. This is especially powerful because you can associate TaskListeners with a SwingCommand instance. For example, you could add a TaskListener which displays a progress animation whenever the command is executed, no matter which Action triggers it.
SwingCommand allows you to add TaskListeners which will receive events on every invocation of the command, or just pass in a TaskListener for a one-off invocation. Similarly, you can also specify an Executor to determine which background thread will run your command, but you can also pass in an Executor for a specific invocation. You can also create your SwingCommand with a set of default parameters - but keep the option of passing in parameters for each invocation.
SwingCommand's integration with the java.util.Concurrent package and support for Executor means it is easy to keep control of the background tasks which run within your Swing app. For example, you could associate a fixed size thread pool Executor with all SwingCommands in your application, to limit the number of concurrent background Tasks.
Finally, SwingCommand has support for the Composite pattern - A composite command can define a CompositeTask which combines other commands together, and executes them in sequence.
SwingCommand is fully interoperable with the rest of Swing. You can invoke SwingCommands from your Swing Action classes, but SwingCommands can also be launched from background threads, and triggered easily from elsewhere in your UI code - they are not restricted to use in Actions, and they don't have to be triggered by ActionEvents. Lastly, not all of your SwingCommands have to launch background tasks. It is easy to define a SwingCommand which just executes in the Swing Event thread (EDT).
Licensing
SwingCommand is released under the open source Apache License (version 2)
This means it is freely available for commercial or non-commercial use, without the obligation to disclose modifications
Quick Links:

