Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Closing my application (frame) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Closing my application (frame)" Watch "Closing my application (frame)" New topic
Author

Closing my application (frame)

Zeki Karaca
Greenhorn

Joined: Oct 02, 2009
Posts: 20
I have a question about a code snippet. See below:




Is this piece of code necessary for closing my application when pressing the 'X' on a frame? Is it not sufficient enough to do:



Can anyone explain this a little bit for me?


SCJA | SCJP 6.0 | ...
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3578
    
  14

setDefaultCloseOperation is sufficient. You can use the windowClosing method to define custom close operations, for instance, if you want to ask the user for confirmation before closing.
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
The DefaultCloseOperation option is perfectly fine for simple straight forward "hey close my program now" usage. WindowListeners are useful for more specific actions when closing a window. Say for instance you want the user to verify his close decision prior to actually exiting the program or if you want to do something like a forced save of data immediately prior to closing. Things of this nature are reasons why you might decided that just setting the DefaultCloseOperation is not enough.


Everyday in every way, we get a little better.
Zeki Karaca
Greenhorn

Joined: Oct 02, 2009
Posts: 20
Thanks for your reply. But will this terminate and exit correctly? I also read somewhere that using DISPOSE_ON_CLOSE is better than EXIT_ON_CLOSE?
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
http://www.coderanch.com/t/340183/GUI/java/DISPOSE-CLOSE-vs-EXIT-CLOSE
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Bobby Smallman wrote:TWindowListeners are useful for more specific actions when closing a window. Say for instance you want the user to verify his close decision prior to actually exiting the program or if you want to do something like a forced save of data immediately prior to closing.

Although in the latter case you might be better using a shutdown hook. That way you don't need to worry about how the application was closed (X button, Exit menu option, etc).
Zeki Karaca
Greenhorn

Joined: Oct 02, 2009
Posts: 20
Thanks Bobby that certainly clarified my doubts and answered my question!
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Matthew Brown wrote:
Although in the latter case you might be better using a shutdown hook. That way you don't need to worry about how the application was closed (X button, Exit menu option, etc).


Absolutely, I suppose I should have made mention to shutdown hooks....Sometimes I speak too generally for my own good!
Zeki Karaca
Greenhorn

Joined: Oct 02, 2009
Posts: 20
Ok, now i'm curious again. What do you guys mean by "shutdown hooks"?
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
http://onjava.com/onjava/2003/03/26/shutdownhook.html

A shutdown hook is another way to initialize closing program actions...check out the link for details.
Zeki Karaca
Greenhorn

Joined: Oct 02, 2009
Posts: 20
Thanks again for the quick reply. I will read that in a minute.
 
Consider Paul's rocket mass heater.
 
subject: Closing my application (frame)
 
Similar Threads
update database when session got invalidated
Making instantiated frames independent
Problem while calling main() method.
basic question from GUI beginner
Memory leak by "closing" JFrame?