Is there a way to remove the close button (\"X\") from the JDialog
title bar?
Here is my experience:
setUndecorated(true)
: Made the whole Dialog
invisible.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
: This didn't change the behavior at all. My dialog box still closed. Setting the default close operation to DO_NOTHING_ON_CLOSE
delegates the close operation to the windowClosing()
method of a registered WindowListener
.What worked for me was:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
//Remove any existing WindowListeners
for (WindowListener wl : this.getWindowListeners()) {
this.removeWindowListener(wl);
}
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if ("Optional condition") {
JOptionPane.showMessageDialog(null, "You cannot close this window");
}
}
});