How to capture a JFrame's close button click event?
Asked Answered
C

6

103

I want to call a method confirmExit() when the red close button of the title bar of a JFrame is clicked.

How can I capture that event?

I'd also like to prevent the window from closing if the user chooses not to proceed.

Convoke answered 1/2, 2012 at 9:27 Comment(0)
A
172
import javax.swing.JOptionPane;
import javax.swing.JFrame;

/*Some piece of code*/
frame.addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
        if (JOptionPane.showConfirmDialog(frame, 
            "Are you sure you want to close this window?", "Close Window?", 
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
            System.exit(0);
        }
    }
});

If you also want to prevent the window from closing unless the user chooses 'Yes', you can add:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Aragon answered 1/2, 2012 at 9:34 Comment(1)
If want the program to only close this frame and not entirely stop, use frame.dispose() instead of System.exit(0)Suzerainty
D
26

Override windowClosing Method.

public void windowClosing(WindowEvent e)

It is invoked when a window is in the process of being closed. The close operation can be overridden at this point.

Deemphasize answered 1/2, 2012 at 9:28 Comment(2)
Will this work if the user closes the application (for example, by hitting Cmd + Q on a Mac)? Does the class that implements this method have to declare implements WindowListener for this code to work? If so, it might be good to include that in the answer. Otherwise this is an elegant solution.Salep
@brandaemon If you add this line of code to the top of your main() method "System.setProperty("apple.eawt.quitStrategy", "CLOSE_ALL_WINDOWS");" then it will call the windowListener when Cmd + Q is pressed on mac.Eritrea
N
2

This may work:

jdialog.addWindowListener(new WindowAdapter() {
    public void windowClosed(WindowEvent e) {
        System.out.println("jdialog window closed event received");
    }

    public void windowClosing(WindowEvent e) {
        System.out.println("jdialog window closing event received");
    }
});

Source: https://alvinalexander.com/java/jdialog-close-closing-event

Nonillion answered 4/9, 2017 at 15:31 Comment(0)
E
1
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

also works. First create a JFrame called frame, then add this code underneath.

Earley answered 1/2, 2012 at 9:33 Comment(7)
But this does not execute the confirmExit() method as the OP would like itAbolition
It does the same thing thoughEarley
No, I don't want to simply close the program. I want to do something before it happens.Convoke
oh sorry, I didn't know that.Earley
Although this is not the right answer, for someone who just want a simple solution this is useful. +1.Wretched
"What I want to do is to call ... confirmExit() when [the X] is clicked." And the previous comment is unrelated to OQ.Clothesbasket
This is exactly the contrary, add frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); to have a control with confirmExit.Moony
E
1

This is what I put as a menu option where I made a button on a JFrame to display another JFrame. I wanted only the new frame to be visible, and not to destroy the one behind it. I initially hid the first JFrame, while the new one became visible. Upon closing of the new JFrame, I disposed of it followed by an action of making the old one visible again.

Note: The following code expands off of Ravinda's answer and ng is a JButton:

ng.addActionListener((ActionEvent e) -> {
    setVisible(false);
    JFrame j = new JFrame("NAME");
    j.setVisible(true);
    j.addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent windowEvent) {
            setVisible(true);
        }
    });
});
Expectant answered 20/11, 2017 at 21:20 Comment(0)
A
0

Try this:

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

It will work.

Aguilar answered 18/4, 2016 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.