• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What makes Java wait on a JFrame?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic