This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to stop program execution

 
Manel Ibanez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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"?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49783
69
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic