Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling the close x in a JOptionPane

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

like image 943
Mike2012 Avatar asked Mar 01 '23 00:03

Mike2012


2 Answers

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?

like image 185
nandokakimoto Avatar answered Mar 07 '23 12:03

nandokakimoto


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();
like image 27
Samsky Avatar answered Mar 07 '23 14:03

Samsky