• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why InvokeLater

 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});

When create a Frame using netBeans, netBeans automatically add this code.
As I understand from previous reading, there is always Event-Dispatch thread where events are fired and handled. I assumed that EventQueue is the responsible to queue and fire events on the event-dispatch thread!!

Is that correct?

The run method in the above code is executing in Event-Dispatch thread...Why I would want to do that?? what happens if code runs in the main thread??

I think this is common question, however I did not find it!!

Thanks
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Costa lamona:
Hi

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});

When create a Frame using netBeans, netBeans automatically add this code.
As I understand from previous reading, there is always Event-Dispatch thread where events are fired and handled. I assumed that EventQueue is the responsible to queue and fire events on the event-dispatch thread!!

Is that correct?

The run method in the above code is executing in Event-Dispatch thread...Why I would want to do that?? what happens if code runs in the main thread??

I think this is common question, however I did not find it!!

Thanks


If you read the documentation for EventQueue.invokeLater, you will find:
Causes runnable to have its run method called in the dispatch thread of the system EventQueue. This will happen after all pending events are processed.

The doco for SwingUtilities.invokeLater, which is just a cover for EventQueue.invokeLater, explains this in much more depth. I suggest you read it for yourself, and come back if there's anything you still don't get.
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Costa lamona:

The run method in the above code is executing in Event-Dispatch thread...Why I would want to do that?? what happens if code runs in the main thread??

I think this is common question, however I did not find it!!


This isn't documented as well as it should be. Here is some relevant info from the Swing javadocs, and here is some history on this.
[ May 19, 2008: Message edited by: Brian Cole ]
 
Suraj Chandran
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java's GUI is strictly single-threaded.
All GUI related things in java should always go through a single thread. The thread is our legendary "AWT-EventQueue-0" . Hence all GUI related actions should necessarily go through the AWT Event thread. If not so you may end up in a deadlock. For small programs, this might never happen. But for a huge java application if you try frame.setVisible(true) kind of thing in main thread, you will soon find yourself searching a new job. What invokeLater() does is to post your Runnable in the AWT thread's event queue. So the code in your run method will be executed in the AWT-Eventqueue thread. I hope you get the idea. You can always get more information on google.
Cheers, Suraj
 
Alistair Parler
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From this link:

Threading in Swing

(refer to: "Single Thread Rule")

it seems that the first time you create a JFrame (and make it visible), you are 'allowed' to do this from the main thread. However, once it is "realized" (with show(), setVisible(true), pack()) and you wish to change its state, you must do so from within the AWT event dispatch thread.

Hope that helps (it helped me!).


Thanks,
Alistair
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic