JoptionPane ShowConfirmDialog

47,105

Solution 1

Try setting this,

app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)

[Edited]

So, your code will become something like this,

public static void main(String args[]) {
    ButtonTest app = new ButtonTest();
    app.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            int reply = JOptionPane.showConfirmDialog(null,
                    "Really Quit ?", "Quit", JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION)
                System.exit(0);
        }
    });
    app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    app.setSize(640, 480);
    app.setVisible(true);
}

[Explanation]

You might be thinking that why it is like that. The behaviour of windows close button for JFrame, unlike Frame,is to hide the window. Therefore, it will hide/close the window anyway. But when you specify that it must also exit the program, when the user click yes. Then, besides closing the window, it also exits the program. And when user clicks no, it does nothing but closes the window anyway. Hence, you must tell it explicitly that DO_NOTHING_ON_CLOSE.

[Docs]

Unlike a Frame, a JFrame has some notion of how to respond when the user attempts to close the window. The default behavior is to simply hide the JFrame when the user closes the window. To change the default behavior, you invoke the method setDefaultCloseOperation(int). To make the JFrame behave the same as a Frame instance, use setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE).

Ref: JFrame docs

Solution 2

You Don't need the else in this case

Solution 3

If you will ask me, I will go with, on YES SELECTION instead of abruptly closing my Application with System.exit(0), I will choose the gracious way of closing my Application, by using frameObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) and on NO SELECTION , I will go for frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE). Here is one sample program for your help :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ApplicationCloseExample
{   
    private void displayGUI()
    {
        final JFrame frame = new JFrame("Application Close Example");

        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                int result = JOptionPane.showConfirmDialog(
                                frame, "Do you want to Exit ?"
                                , "Exit Confirmation : ", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)               
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                else if (result == JOptionPane.NO_OPTION)   
                    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            }
        });

        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ApplicationCloseExample().displayGUI();
            }
        });
    }
}
Share:
47,105
Eric
Author by

Eric

Updated on July 25, 2020

Comments

  • Eric
    Eric almost 4 years

    I have a Java program. When I run the program, it will give me a GUI which as I attached.

    When I want to close it, it will prompt out a confirm dialog. If I press the Yes button, it will quit the program using System.exit().

    public static void main(String args[])
    {
        ButtonTest app = new ButtonTest( );
        app.addWindowListener( 
            new WindowAdapter( )
            {
                public void windowClosing (WindowEvent e)
                {
                    String message = " Really Quit ? ";
                    String title = "Quit";
                    int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
                    if (reply == JOptionPane.YES_OPTION)
                    {
                        System.exit(0);
                    }
    
                }
            }
        );
    }
    

    If I don't want to quit the program, what can I do? System.continued() ?

  • Eric
    Eric almost 12 years
    but when i click no, the whole program gone but i can see in command prompt, it is still working .
  • Eric
    Eric almost 12 years
    i have erase and i have put the code above. when click no , the program gone also but in comand prompt, it still running.
  • Adeel Ansari
    Adeel Ansari almost 12 years
    @Eric: Updated my answer accordingly.
  • nIcE cOw
    nIcE cOw almost 12 years
    I just realized that using ~JFrame.EXIT_ON_CLOSE~ is same as using ~System.exit(0)~, hence you can use ~JFrame.DISPOSE_ON_CLOSE~ as an alternative :-)