Close JFrame with a "really close?" dialog

623 views Asked by At

I have a JFrame and if I press the close button in the top right corner I invoke a JDialog and ask if the user really wants to close.

This is the ActionListener on the close Button in my main JFrame:

...
//close listener
        addWindowListener(new WindowAdapter() {//invoke "wirklich schließ3n" window if Alt+F4
            @Override
            public void windowClosing(WindowEvent event) {
                MainGuiWindow.this.saveSettings();

CloseDialog cd = new CloseDialog(MainGuiWindow.this); if (cd.getResult()) { System.exit(0); } else { //MainGuiWondow is "setVisible(false)" but still running | I don't know how to fix it MainGuiWindow.this.setVisible(true);//doesn't work } } });

The JDialog has just two buttons (YES/NO) (after pressing any button i call dispose(); in the ActionListeners of the Buttons in the JDialog and after the dispose(); i return the answers: yes returns true no returns false

Can anybody tell me what to do in the else case of my Close ActionListener in my MainGuiWindow. The CloseDialog is closed cause of the dispose(); but the main window is is setVisible(false); but still running

1

There are 1 answers

0
Anptk On

This my help you

You need to set default close operation on the JFrame to “do nothing”:

jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

add a WindowListener for the frame.

jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
    handleClosing();
}
});

And Refer Here