eu.beesoft.gaia.swing
Class SwingWorker

java.lang.Object
  extended by eu.beesoft.gaia.swing.SwingWorker

public abstract class SwingWorker
extends java.lang.Object

Implementation of SwingWorker from SUN with interrupting capability.

Ensures executing of long - running tasks, that are invoked from Swing (AWT event) thread in separate thread rather than in Swing thread. So Swing thread is not blocked, the Swing components can be repainted and the events served.

Class has two important methods: doInBackground() and done(). The first is invoked in separate thread after SwingWorker starts and it does not block Swing thread. This method should be used to long-runnning task executing (such as communication with server). The second is invoked when doInBackground() method finished and it runs in Swing (AWT event thread). This method should be used to set values to Swing components.

Method get() blocks until doInBackground() is running and then returns value computed in this method.

Method interrupt() interrupts the code executing in doInBackground() method.

Usage:
                ...
                // here we are usually in Swing (AWT event) thread
 
                SwingWorker worker = new SwingWorker () {
 
                        protected Object doInBackground () {
                                // this method is running in separate thread
                                // here comes the long-running code
                                // (communication with server, etc.)
                        }
 
                        protected void done () {
                                // this method is running on Swing (AWT event) thread
                                // here comes code to set values to Swing components
                        }
                };
                worker.start();
                ...
 


Constructor Summary
SwingWorker()
          Creates a new instance of SwingWorker.
 
Method Summary
protected abstract  java.lang.Object doInBackground()
          In overriden method in the subclass here comes the long-runnning code.
protected  void done()
          In overriden method in the subclass here comes the code to set values computed in doInBackground() method to Swing components.
 java.lang.Object get()
          Returns value computed in doInBackground() method.
 void interrupt()
          Interrupts the code executing in doInBackground() method.
 void start()
          Starts this SwingWorker (invokes doInBackground() in separate thread).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwingWorker

public SwingWorker()
Creates a new instance of SwingWorker.

Method Detail

start

public void start()
Starts this SwingWorker (invokes doInBackground() in separate thread).


get

public java.lang.Object get()
Returns value computed in doInBackground() method. This method blocks until doInBackground() is running.

Returns:
a value computed in doInBackground() method (or null, if no value was computed)

interrupt

public void interrupt()
Interrupts the code executing in doInBackground() method.


doInBackground

protected abstract java.lang.Object doInBackground()
In overriden method in the subclass here comes the long-runnning code. This method is invoked in separate thread.


done

protected void done()
In overriden method in the subclass here comes the code to set values computed in doInBackground() method to Swing components. This method is invoked in Swing (AWT event) thread. In this implementation does nothing.