I have constructed a GUI with a JButton and a JTextArea.
When the JButton is pressed I want to execute a method that reads
information from somewhere and prints it to JTextArea.
The method that reads the information takes a few seconds to compute.
I followed the example at
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
And used the class
http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/SwingWorker.java
From this I understood that any work that will take a long amount of
time should be executed in a seperate thread.
So here is what I have come up with
private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String command = e.getActionCommand();
if (command.equalsIgnoreCase("Get Data"))
{
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.
Any help/comments greatly appreciated.
regards,
pat
[snip]
> final SwingWorker worker = new SwingWorker()
> {
[quoted text clipped - 45 lines]
> 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.

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