GUI Commands User GuideVersion 2.1 |
||
Swing WorkersSwingWorkerCommands provide a convenient wrapper for the SwingWorker
class. The API is essentially the same as SwingWorker, but includes a few command specific additions.
The SwingWorkerCommand automatically disables during the operation and can optionally
activate the glass pane. The command also provides a The following is a basic example:
SwingWorkerCommand command = new SwingWorkerCommand("my-command")
{
@Override
protected boolean beforeStarting()
{
// this method runs on the EDT, you can perform
// checks and return false to cancel the operation.
return true;
}
protected Object doInBackground()
{
// do long running operation and optionally
// return the result
return ...;
}
@Override
protected void finished(Object result)
{
//update the UI
...
}
@Override
protected void failed(ExecutionException e)
{
// handle the exception
...
}
@Override
protected void cancelled(Exception e)
{
// The exception is either a CancellationException
// or InterruptedException
...
}
}
DisablingSwingWorkerCommands will automatically disable while the background process is in opertaion.
You can change this default behaviour using the
command = new SwingWorkerCommand("my-command");
command.setDisabledWhileWorking(false);
Glass Pane IntegrationSwingWorkerCommands can be configured to activate the glasspane during operation. To do this you need to do the following:
The following is an example: CommandContainer container = ...; GlassPaneStrategy glassPaneStrategy = ...; // set the strategy on the container. container.setGlassPaneStrategy(glassPaneStrategy); // create the worker and set the busy mode. SwingWorkerCommand worker = new SwingWorkerCommand("my-worker") {...}; worker.setActivateGlassPane(true); // bind the worker so it can find the glass pane strategy to use. worker.bind(container); Glass Pane StrategiesWorker commands activate and deactivate the glass pane using a The strategy defines two methods, one for activating and one for deactivating the glass pane. The
methods take the invoker as the only parameter. SwingWorkerCommands pass themselves
as the invoker allowing the strategy to monitor progress and optionally display chunking
information. Since the invoker is of type GUI Commands provides a simple strategy that beeps if the user clicks while it's activated. The following is an example: DefaultGlassPaneStrategy strategy = new DefaultGlassPaneStrategy(); frame.setGlassPane(strategy.getGlassPane()); |
||