Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Close JFrame

 
Daniela Ch
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's as simple as this:

Eugene.
 
Daniela Ch
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow...that was fast...
thank you soo much....
 
Trent DiBacco
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Trent
 
Robbie shi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
-----------------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic