4

I would like to disable the close x in the upper left corner of my JOptionPane how would I do this?

3
  • 2
    Why do you want to do that? JOptionPane should normally have a "Cancel" or "No" button. You should make hitting the close button behave consistently with those. Nov 20, 2009 at 21:12
  • I have to agree with ykaganovich. This seems like a bad idea from a user-experience perspective. If your user chooses no option, perhaps it would be better to a) do nothing or b) choose a reasonable default for them.
    – ssakl
    Nov 23, 2009 at 17:30
  • I suppose I may have misstated the question what I really wanted to do was to get rid of the close button (this is what the apple human interface guidelines suggest and my boss is in love with HIG). In the end it ended up just being easier to give the close button the same functionality as the cancel button so this is what I did. Thanks.
    – Mike2012
    Nov 24, 2009 at 19:34

4 Answers 4

5

Michael,

I don't know how to disable the Close[x] button. Alternatively, you can do nothing when user clicks on it. Check the code below:

JOptionPane pane = new JOptionPane("message");
JDialog dialog = pane.createDialog(null, "Title");
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setVisible(true);

Is it reasonable for you?

3
  • I think it would be but how would I get the return value of the pane if it encapsulated inside the JDialog?
    – Mike2012
    Nov 23, 2009 at 22:39
  • If you look at the answer I provided which is straight from Sun, it shows how to do what you're looking for. Basically you'd have to keep a reference to the JOptionPane in order to retrieve the return value. Nov 24, 2009 at 14:33
  • I like your answer. For me works like a charm. Thanks
    – jotapdiez
    Jun 1, 2015 at 1:50
2

you can overwrite your exit button by a cancel button declared in JOptionPane and handle your cancellation operation accordingly:

JOptionPane optionPane= new JOptionPane("message", JOptionPane.OK_CANCEL_OPTION);

final JDialog dialog = optionPane.createDialog(null, "Input");
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() { 
@Override public void windowClosing(WindowEvent e) { 
    optionPane.setValue(JOptionPane.CANCEL_OPTION);
}
});

if (JOptionPane.CANCEL_OPTION!= ((Integer) optionPane.getValue()).intValue())
                    throw new myCancellationException();
1

You could always just show the dialog again when the user tries to close it without selecting an option. There's an example of how to override the default closing behavior at sun.com. Look under "Stopping Automatic Dialog Closing" and they have the following code:

final JOptionPane optionPane = new JOptionPane(
                "The only way to close this dialog is by\n"
                + "pressing one of the following buttons.\n"
                + "Do you understand?",
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.YES_NO_OPTION);

final JDialog dialog = new JDialog(frame, 
                             "Click a button",
                             true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(
    JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
        setLabel("Thwarted user attempt to close window.");
    }
});

optionPane.addPropertyChangeListener(
    new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            String prop = e.getPropertyName();

            if (dialog.isVisible() 
             && (e.getSource() == optionPane)
             && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
                //If you were going to check something
                //before closing the window, you'd do
                //it here.
                dialog.setVisible(false);
            }
        }
    });
dialog.pack();
dialog.setVisible(true);

int value = ((Integer)optionPane.getValue()).intValue();
if (value == JOptionPane.YES_OPTION) {
    setLabel("Good.");
} else if (value == JOptionPane.NO_OPTION) {
    setLabel("Try using the window decorations "
             + "to close the non-auto-closing dialog. "
             + "You can't!");
}

Using that code, you could easily adapt the commented section to only allow the window to be closed when the user has clicked one of the available options and not the close button.

0

I'm not sure if there is a way to do this in JOptionPane.

Usually when people want more flexibility than JOptionPane offers (it's basically a bunch of static factories for a few dialogs), they write their own dialogs using JDialog.

JDialog offers the inherited method setUndecorated, which eliminates the X altogether. It's more work but you can make your dialog look however you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.