File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to stop program execution" Watch "How to stop program execution" New topic
Author

How to stop program execution

Manel Ibanez
Greenhorn

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.

Thanks.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

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

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
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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: http://aspose.com/file-tools
 
subject: How to stop program execution