I want to make my Java Application to call my own custom made function when the “close” cross button is pressed. as far as i see there may be no way since setDefaultCloseOperation has no overloads at all.
Any idea how this can be achieved?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
All the above suggestions are correct in that you need to use a WindowListener.
However all the answer are also incomplete in that they forget to mention that you may also want to add:
This will allow your code to take full control of the window closing process as the window will not automatically close unless you tell it to (generally by using the dispose() method on the frame). This allow you to promt the user for a confirmation to close the window or not.
Closing an Application has a simple API that allows you to create a simple Action that is executed when the window is closed. It manages the close operation and the window listener code for you.