Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2004

Tip: Looking for answers? Try searching our database.

Moving a time-consuming task out of the event-dispatching thread

Thread view: 
Patrick - 27 Sep 2004 21:06 GMT
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
Nigel Wade - 29 Sep 2004 10:18 GMT
[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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.