• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invokeLater()

 
Andrew Collins
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jodas !
Life can be simple
 
dean tomlinson
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic