From http://groups-beta.google.com/group/comp.lang.java.gui/msg/c7ff91e306e0aaa0

Patrick wrote:

[snip]

> final SwingWorker worker = new SwingWorker()
> {
> private Vector dataObjects;

> public Object construct ()
> {
> //...code that might take a while to execute is
> here...

> //SomeObject has a method getAllData() that returns a
> Vector of dataObjects
> //getAllData() takes in the order of a few seconds to
> complete

> SomeObject so = new SomeObject();
> dataObjects = so.getAllData();

> return dataObjects;
> }

> public void finished ()
> {
> Enumeration enum = dataObjects.elements();

> while (enum.hasMoreElements())
> {
> textArea.append(((DataObject)
> enum.nextElement()).toString() + "\n");
> }
> };

> worker.start();
> }
> }
> }
> }

> My question is what is the proper way print the results from the
> method getAllData() to the textArea.

> The webpage says that any updating to the GUI components can be
> carried out in the finished() method. So is this where I should print
> out the results to the textArea?

> If so, what is the best/correct way to pass the results from the
> construct() method to the finished() method. I merely created a
> variable ,private Vector dataObjects, which construct() writes into
> and finished() reads from.

There's no need to "pass" anything from construct() to finished(). Both
those methods are part of the same class, and methods of the class have
access to the class instance data dataObjects.

The nice thing which SwingWorker does for you is to ensure that the
finished() method is always run when the time consuming process has
completed, and also that it is run in the EDT (by
SwingUtilities.invokeLater()) so that finished() can safely update GUI
components.

The value returned by construct() is used internally within the SwingWorker
class to hold a "value" for the object which you can obtain by invoking the
accessor method get(), but this will block until the time consuming process
is completed.

-- 
Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    n...@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555
