Remove "X" button in Swing JDialog

JavaSwing

Java Problem Overview


Is there a way to remove the close button ("X") from the JDialog title bar?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

Here is my experience:

  • Tried using setUndecorated(true): Made the whole Dialog invisible.
  • Tried 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");
        }
    }
});

Solution 5 - Java

At a guess, set it to be PL&F decorated and remove the component by name.

Solution 6 - Java

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]);
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrichsView Question on Stackoverflow
Solution 1 - JavaHuxiView Answer on Stackoverflow
Solution 2 - Javathedude19View Answer on Stackoverflow
Solution 3 - JavaKy.View Answer on Stackoverflow
Solution 4 - JavaRyuuView Answer on Stackoverflow
Solution 5 - JavaTom Hawtin - tacklineView Answer on Stackoverflow
Solution 6 - JavakrzydynView Answer on Stackoverflow