user3026693
user3026693

Reputation: 133

making JOptionPane disappear

public void windowClosing (WindowEvent e)
    {
        JFrame frame = new JFrame();

        int confirm = JOptionPane.showConfirmDialog (frame, "Exit game?", "Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (confirm == JOptionPane.YES_OPTION)
        {
            dispose ();
        }
        else
        {
            frame.setVisible(false);
        }
    }

So when the user clicks on the close button, a JOptionPane pops up. When the user clicks on "No" the JOptionpane is supposed to disappear and then return to the frame it was originally displaying, but with my code, even when I click on No, both frames, the one for the JOptionPane and the one it sits on, disappear.

One thing:

Upvotes: 0

Views: 1669

Answers (3)

hbsrud
hbsrud

Reputation: 353

It's really simple, your frame disappear because you say that it should not be visible, just remove that else:

public void windowClosing (WindowEvent e) {         
   int confirm = JOptionPane.showConfirmDialog (this, "Exit game?", "Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

   if (confirm == JOptionPane.YES_OPTION) {
      dispose();
   }
}

EDIT:

Also replace setDefaultCloseOperation (EXIT_ON_CLOSE); with setDefaultCloseOperation (DO_NOTHING_ON_CLOSE);, else the main frame would close regardless of what happens in the windowClosing method.

Upvotes: 2

Paul Samsotha
Paul Samsotha

Reputation: 209132

Don't create a new JFrame in the method. That's why you have random frame from now where.

public void windowClosing (WindowEvent e)
{
    JFrame frame = new JFrame();

Pass the reference of the JFrame in question. If the code above is from a JDialog you can do something like this

public class MyDialog extends JDialog {
    public MyDialog(final JFrame frame, boolean modal) {
        super(frame, modal);

        public void windowClosing (WindowEvent e) {

            int confirm = JOptionPane.showConfirmDialog (frame, "Exit game?", "Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (confirm == JOptionPane.YES_OPTION) {
                dispose ();
            }

        }
    }
}

And just instantiate the JDialog like this from your GUI class with the JFrame

MyDialog dialog = new MyDialog(thisFrame, true);

Side Note

Why Even have a JOptionPane and a JDialog? Ultimately, a JDialog is just a custom JOptionPane, they have the same functionality.


EDIT

If you just want to pass the JFrame class as reference to the JOPtionPane just pass

MyFrameClass.this

instead of a new JFrame()


UDPATE

Test out this program using a simple custom JDialog.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JDialogExample extends JFrame {

    public JDialogExample() {
        JButton exit = new JButton("Do you want to Exit?");
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new HelloDialog(JDialogExample.this, true);
            }
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(exit);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class HelloDialog extends JDialog {

        public HelloDialog(final JFrame frame, boolean modal) {
            super(frame, modal);

            JButton exit = new JButton("EXIT");
            JButton cancel = new JButton("CANCEL");

            setLayout(new GridLayout(2, 1));
            JPanel panel = new JPanel();
            panel.add(exit);
            panel.add(cancel);

            add(new JLabel("Do you want to exit?", JLabel.CENTER));
            add(panel);

            exit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            cancel.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });

            pack();
            setLocationRelativeTo(frame);
            setVisible(true);

        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JDialogExample();
            }
        });
    }
}

Upvotes: 1

Maroun
Maroun

Reputation: 96016

Just don't put else if you don't want to hide the frame. The JOptionPane will disappear by itself, whether you click yes or no.

Upvotes: 1

Related Questions