• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does main call invoke later?  RSS feed

 
Siegfried Heintze
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm noticing this in the sample swing programs:

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
constructGUI();
}
});
}
Is this a best practice? Why? Won't it run fine by having main call constructGUI directly?
Thanks,
Siegfried
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Siegfried Heintze:
I'm noticing this in the sample swing programs:

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
constructGUI();
}
});
}
Is this a best practice? Why? Won't it run fine by having main call constructGUI directly?


I wrote about this in a blog entry a few month ago. You might want to take a look.
 
Siegfried Heintze
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a similar recommendation that one call new Frame in a separate thread for a non-swing AWT application (such as a applet)?
Thanks,
Siegfried
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It's recommended to execute GUI related task in Event-Dispatch-Thread with SwingUtilities.invoke*() method. In the old version of Java Tutorial, they create frames directly in the main thread. They've updated all the examples the same way with your sample program.
 
Siegfried Heintze
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us suppose my reason for not using swing is that I am writing an Applet for a public web page and don't want the extra complication or bandwidth of dynamically downloaded the swing jars on demand.

If I call swingutilities.invoke* won't I have just shot myself in the foot?

Are there any other reasons for only using AWT not using swing?


Thanks,
Siegfried
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Siegfried Heintze:
Let us suppose my reason for not using swing is that I am writing an Applet for a public web page and don't want the extra complication or bandwidth of dynamically downloaded the swing jars on demand.


Swing is included in the JRE on the client machines. There are no bandwidth issues in using swing in applets.

This has been true since JDK 1.2 released in 1998. (Before that there were jar download issues as you describe, but that was way back in the stone age.)

If I call swingutilities.invoke* won't I have just shot myself in the foot?


I don't see how. Using them or not doesn't change what code is executed or which jars are required. They just change which thread does the executing (and when).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!