Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Closing my application (frame)

 
Zeki Karaca
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stephan van Hulst
Bartender
Pie
Posts: 5415
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zeki Karaca
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/340183/GUI/java/DISPOSE-CLOSE-vs-EXIT-CLOSE
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bobby that certainly clarified my doubts and answered my question!
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now i'm curious again. What do you guys mean by "shutdown hooks"?
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for the quick reply. I will read that in a minute.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic