Reputation: 4769
Is there a way to remove the close button ("X") from the JDialog
title bar?
Upvotes: 63
Views: 67644
Reputation: 483
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");
}
}
});
Upvotes: 6
Reputation: 4301
You can remove the whole dialog title by calling dialog.setUndecorated(true)
but this means that the dialog can't be moved anymore.
You can also execute dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
to prevent that the button does anything.
Besides that, I don't think that there's a way to remove the X
completely.
Upvotes: 61
Reputation: 32073
As of Java 1.7 (AKA Dolphin or Java 7), you can not disable or remove the close button on a Window. You can remove/disable the maximize button with frame.setResizable(false)
and you can remove the minimize and maximize buttons by using a java.awt.Dialog
or a class that extends it, such as javax.swing.JDialog
. You can remove the title bar, borders, and buttons with frame.setUndecorated(true)
, and you can have full control over the visibility of all buttons in the title bar (while losing some cross-platform compatibility and OS integration) with frame.setDefaultLookAndFeelDecorated(true)
(assuming it's a JFrame or JDialog). This is all the control I see possible with the current JDK.
Upvotes: 12
Reputation: 9
static public void removeButtons(Component c){
if (c instanceof AbstractButton){
String accn = c.getAccessibleContext().getAccessibleName();
Container p=c.getParent();
//log.debug("remove button %s from %s",accn,p.getClass().getName());
c.getParent().remove(c);
}
else if (c instanceof Container){
//log.debug("processing components of %s",c.getClass().getName());
Component[] comps = ((Container)c).getComponents();
for(int i = 0; i<comps.length; ++i)
removeButtons(comps[i]);
}
}
Upvotes: -3
Reputation: 2673
I believe that you can call dialog.setUndecorated(true)
to remove the title bar. Not sure about just the 'X' though.
Removing the 'X' may not be a great idea, though, as you want your users to be able to close the dialog easily.
Best bet is to control what happens when the users clicks the 'X' by using dialog.setDefaultCloseOperation
or a WindowListener
.
Upvotes: 14
Reputation: 147144
At a guess, set it to be PL&F decorated and remove the component by name.
Upvotes: 1