*
The moose likes Swing / AWT / SWT and the fly likes Why InvokeLater Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why InvokeLater" Watch "Why InvokeLater" New topic
Author

Why InvokeLater

Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
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


SCJP 5
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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 ]

bitguru blog
Suraj Chandran
Ranch Hand

Joined: May 20, 2008
Posts: 53
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


I guess there are couple of design issues in this classic software the GOD coded.
Alistair Parler
Greenhorn

Joined: May 24, 2009
Posts: 11
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why InvokeLater
 
Similar Threads
Help in a simple fan project
retrieving event dispatch thread
How is modality implemented?
Animation Applet
Event Dispatch Thread