Create custom operation for setDefaultCloseOperation?
Asked Answered
L

4

21

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?

Landfall answered 21/5, 2011 at 19:46 Comment(0)
C
31

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:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(...);

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.

Corrugate answered 22/5, 2011 at 2:12 Comment(0)
A
37

maybe this one, but before that read tutorial WindowListener posted by Howard, there are some/another options

WindowListener exitListener = new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        int confirm = JOptionPane.showOptionDialog(
             null, "Are You Sure to Close Application?", 
             "Exit Confirmation", JOptionPane.YES_NO_OPTION, 
             JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {
           System.exit(0);
        }
    }
};
frame.addWindowListener(exitListener);
Alcahest answered 21/5, 2011 at 20:19 Comment(0)
C
31

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:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(...);

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.

Corrugate answered 22/5, 2011 at 2:12 Comment(0)
D
8

You can add a window listener for the frame:

frame.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) {
     onExit();
   }
  });

...
public void onExit() {
  System.err.println("Exit");
  System.exit(0);
}
Devotional answered 21/5, 2011 at 19:51 Comment(0)
T
4

You can add a WindowListener to your frame (see e.g. here).

Talbott answered 21/5, 2011 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.