• Post Reply Bookmark Topic Watch Topic
  • New Topic

What makes Java wait on a JFrame?  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

With a simple "Hello World" application, once the println is executed the application exits and the process goes away.

If a simple Frame application is executed, the Frame is displayed, the println is executed but the application does not exit.




It isn't that Java is aware an object exists because if I create a basic non-swing, non-gui object it will exit right after the println.

Q1. What is it that causes Java NOT to exit after creating the JFrame?
Q2. What type of object(s) when created will cause the application to continue running?
Q3. What would I do if I wanted the println statement to be executed only after the JFrame was closed?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Java program only finishes when all non-daemon threads have finished. See this post for more explanation.
So if a program doesn't exit even when the end of the main method completes, it is because there are non-daemon threads still running.
See the Concurrency in Swing tutorial for details of what threads are running in a Swing application. Especially the section about the Event Dispatch Thread.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should start the EDT yourself, like that:-I have moved the anonymous class out of the invokeLater call so as to make it slightly less incomprehensible.
The invoke Later call starts the EDT, so you can see the EDT shows the frame and the main thread shows the print call.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!