wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Close JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Close JFrame" Watch "Close JFrame" New topic
Author

Close JFrame

Daniela Ch
Ranch Hand

Joined: Dec 13, 2002
Posts: 96
Hi,
I have a JFrame that have a file menu that contains Exit.
I also have a method in my GUI called closeApplication();
I have a hook on the file/exit menu, when the user click on exit I can execute my closeApplication() method.
I want the same behavior for the x on the upper right corner...
The only thing I have found related to this x is
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but this wont allow me to call my closeApplication() method.
Any clue?
daniela
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
the windowClosing method is called when the X button is clicked if you implement the WindowListener interface on your frame. You can add code within that method to accomplish what you want.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Yes, it's as simple as this:

Eugene.
Daniela Ch
Ranch Hand

Joined: Dec 13, 2002
Posts: 96
wow...that was fast...
thank you soo much....
Trent DiBacco
Greenhorn

Joined: May 24, 2002
Posts: 18
You want to do is a WindowEvent to your JFrame and either
implment a WindowListener interface or use a helper class named WindowAdapter. The method that you will want to implement in either of these two types is the "void windowClosing(WindowEvent e)" method.
Here is my example. This will creat a small window with a button in the center, use either the button or the x to close the JFrame, and watch the output in your console.

[ January 03, 2003: Message edited by: Trent DiBacco ]
[ January 03, 2003: Message edited by: Trent DiBacco ]
[ January 03, 2003: Message edited by: Trent DiBacco ]
[ January 03, 2003: Message edited by: Trent DiBacco ]
Daniela Ch
Ranch Hand

Joined: Dec 13, 2002
Posts: 96
thank you Trent
Robbie shi
Greenhorn

Joined: Jan 05, 2003
Posts: 28

I have done this code today ,will it be helpful?
frame.setDefaultCloseOperation.(JFrame.EXIT_ON_CLOSE) is also ok
---
Robbies
-----------------------------
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
http://www.pivotonic.com
-----------------------------
 
Don't get me started about those stupid light bulbs.
 
subject: Close JFrame