• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Why InvokeLater

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 979
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic