File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How to stop program execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to stop program execution" Watch "How to stop program execution" New topic

How to stop program execution

Manel Ibanez

Joined: Aug 12, 2011
Posts: 5
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.

Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

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

Joined: Aug 12, 2011
Posts: 5
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

Joined: Oct 13, 2005
Posts: 46397
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.
I agree. Here's the link:
subject: How to stop program execution
It's not a secret anymore!