JoptionPane ShowConfirmDialog

I have a Java program. When I run the program, it will give me the graphical interface that I am attaching.

When I want to close it, it will display a confirmation dialog box. If I click Yes, it will exit 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 do not want to leave the program, what should I do? System.continued()?

+3
source share
5 answers

In this case, you will no longer need

+3
source

Try to install 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]

, , . JFrame, Frame, . / . , , yes. , , . no, . , , DO_NOTHING_ON_CLOSE.

[]

Frame, JFrame , , . - JFrame, . , setDefaultCloseOperation (int). JFrame , Frame, setDefaultCloseOperation (WindowConstants.DO_NOTHING_ON_CLOSE).

: JFrame docs

+3

, YES SELECTION System.exit(0), , frameObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) NO SELECTION, frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE). :

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();
            }
        });
    }
}
+1

, , "", else , .

else , - NO, JOptionPane.showConfirmDialog() . if.

FYI. System.continue(). .

0

You can add a block else. if you want to run the main method again (which I assume you are doing), it should look like this. You must have some method that you run if the user does not select it, whether it is the main method main(null)or another method.

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);
                } 
                else 
                {
                  //whatever you plan on running instead here, instead of quitting,
                  //main(null) to run the main method, or put another method if you want
                }
            }
        }
    );
}
0
source

All Articles