File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes invokeLater() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "invokeLater()" Watch "invokeLater()" New topic
Author

invokeLater()

Andrew Collins
Ranch Hand

Joined: Mar 28, 2002
Posts: 42
Hi,
In order to create a responsive GUI I considered to let every action extend a so called NewThreadAction. Doing so, the actionPerformed code is executed outside the event dispatching thread and thus the GUI (and the event handling)won't freeze during execution.
NewThreadAction has 2 abstract methods : performAction() and updateGUI(). PerformAction resides the code that will be executed in the seperate thread, updateGUI() updates the GUI.
The Swing Tutorial at Sun's site tells us that painting code - updateGUI() in my example - should only be executed in the Event Dispatching Thread and offers us a.o. the SwingUtilities' invokeLater(Runnable) to do so.
Now, I've tried to work this out but I'm not sure if I got this right.
Comments or suggestions are welcome.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
I just define a private inner class that represents a thread action. For example:

To use it, you just create a new thread:

I guess your code would do the same, but in a more formal way. I think that it might be an overkill for this assignment, though. However, in the real app with many action buttons (or other controls), it looks like something to be used.
Be careful though, -- once you allow the actions to run on different threads, you might have sync problems with your Data in local mode, if you didn't implement lock()ing for the local mode.
Eugene Kononov.
Andrew Collins
Ranch Hand

Joined: Mar 28, 2002
Posts: 42
Hi Eugene,
Thanks for the response.
I consider your solution as a good alternative although I have a question for you. The Swing tutorial strongly recommend us to execute painting code in the event handling thread. IMHO this means that the manipulating of your status label should be executed via SwingUtilities.invokeLater(), shouldn't it ? At least, this is how I understand it.
Greetings,
A
Eduard Jodas
Ranch Hand

Joined: May 14, 2002
Posts: 80
Hi,
I think you are right, A. C.
I have experienced some problems when updating the GUI from other thread than the EventDispatchThread, mainly when closing JDialogs at the same time, but this problems are not very common.
Personally I'm going to use the invokeLater, but not sure if I would lose points if not.
Andrew Collins
Ranch Hand

Joined: Mar 28, 2002
Posts: 42
Hi,
I would also use the InvokeLater method, but ...
what if the user clicks the button again before this InvokeLater method got invoked ? f.e. user clicks the button twice ...
I think it gets too complicated if we want to fix this. Maybe it isn't such a good idea to execute the actions in a seperate thread
Does anyone has the answer ?
A
[ May 23, 2002: Message edited by: Andrew Collins ]
Eduard Jodas
Ranch Hand

Joined: May 14, 2002
Posts: 80
The first thing you must do before starting your thread is to disable the buttons and other listeners you have on your GUI.
The last thing you must do before finishing your thread is to enable again all your buttons and other listeners (using invokeLater).
Andrew Collins
Ranch Hand

Joined: Mar 28, 2002
Posts: 42
Thanks Jodas !
Life can be simple
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
i haven't programmed any of my gui code using seprate threads. can anyone who has passed the assignment advise me if they too did not include such functionality, or of this is is possibly out of scope for this assignment ?
i know there are many people on this forum who say, keep things simple and leave out what is not explicitely asked for.
thanks a lot - dean
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: invokeLater()
 
Similar Threads
Worker Threads
simulation of trading application, thread safety issues ?
Swing worker thread
Why are two diff. Tasks/Threads ending up on the same thread?
Event Dispatch Thread