• 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

How to stop program execution

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've written this simple first hello world program:

The program shows a window with the hello world, but when I close the window, the program's still alive. I supose I have to trap somehow the close window event and then exit the app somehow. Can anybody kindly point me in the right direction?
Also, I really don't see in wich point the event pool is set, and if there's a main window or something. I'm following tutorials and that's what I've managed to come with as a first Swing example, so if anyone can give a brief explanation, it'll be much appreciated.

Thanks.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can trap the close window event - look at the addWindowListener() method in JFrame. But there's an easier way that will do what you want here:
 
Manel Ibanez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that was useful. Just out of curiosity, if I set the window to DISPOSE_ON_CLOSE, the window is disposed as expected, and then the program ends equally. Am I right to assume that the internal event pool stops execution when there are no windows or objects alive? If that's true, what's the default status for a window, something like "HIDE_ON_CLOSE"?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Yes, the default is HIDE_ON_CLOSE. It is mentioned in the API documentation. There is more about Swing threads here.

Beware of EXIT_ON_CLOSE; if there are several Threads running, they will all be terminated, and might leave something (eg saving to a file) incomplete, corrupting your records.

I think this discussion would sit better on the Swing forum: Moving.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic