Sorry if there is a question like this already but I couldn't find it. I found things like how to create a button listener and people create a button and perform action on it. I want when the close button ( x button ) is pressed a warning window to pop-up and say that the project is not saved. I couldn't find how to access the close button. How to use a button listener with the close button? Hope made myself clear. Thanks
Asked
Active
Viewed 346 times
0
-
Look through the answers to [OnExit Event For a Swing Application?](http://stackoverflow.com/questions/2467070/onexit-event-for-a-swing-application). – PM 77-1 Nov 07 '13 at 01:38
-
2I think this link is what your are looking for :) http://stackoverflow.com/questions/9093448/do-something-when-the-close-button-is-clicked-on-a-jframe – a_mid Nov 07 '13 at 01:42
-
thanks, it seems I couldn't google it right .. – Nick Nov 07 '13 at 02:08
2 Answers
1
You need to add a WindowAdapter
to your JFrame
.
myFrame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
// do something
}
});
Now, every time someone presses the close button, the windowClosing()
method will be called. Check if the user has saved the work. If not, either auto-save it like or promprt user to save it.

An SO User
- 24,612
- 35
- 133
- 221
1
overload the close/exit action
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
Create custom operation for setDefaultCloseOperation?
define a method separately and add listener for close action.