Remove the X button in Swing JDialog

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

+59
java swing
Jun 02 '09 at 21:45
source share
6 answers

You can remove the entire title of the dialog by calling dialog.setUndecorated(true) , but this means that the dialog can no longer be moved.

You can also do dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) so that the button does nothing.

Also, I don't think there is a way to completely remove X

+57
Jun 02 '09 at 22:39
source share
β€” -

I believe that you can call dialog.setUndecorated(true) to remove the title bar. Not sure about the β€œX” only.

Removing the β€œX” may be a great idea, as you want your users to be able to easily close the dialog box.

It’s best to control what happens when users click the "X" button using dialog.setDefaultCloseOperation or WindowListener .

+12
Jun 02 '09 at 22:42
source share

Starting with Java 1.7 (AKA Dolphin or Java 7), you cannot disable or close the close button in a window. You can remove / disable the maximize button with frame.setResizable(false) , and you can remove the minimize and maximize buttons using java.awt.Dialog or a class that extends it, like javax.swing.JDialog . You can remove the title bar, borders and buttons using frame.setUndecorated(true) , and you can completely control the visibility of all the buttons in the title bar (if you lose some cross-platform compatibility and OS integration) using frame.setDefaultLookAndFeelDecorated(true) (provided that it is JFrame or JDialog). This is all that I see possible with the current JDK.

+9
Sep 27 '10 at 6:28
source share

Here is my experience:

  • Tried to use setUndecorated(true) : made the whole Dialog invisible.
  • Tried setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) : this didn't change the behavior at all. My dialog is still closed. Setting the default close operation to DO_NOTHING_ON_CLOSE delegates the close operation to the windowClosing() method of the registered WindowListener .

What worked for me:

 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"); } } }); 
+4
Sep 21 '16 at 13:57
source share

Guess set it as PL & F, decorating and removing the component by name.

+1
Jun 02 '09 at 22:35
source share
 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]); } } 
-3
Jun 19 '12 at 21:30
source share



All Articles