Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "X" button in Swing JDialog

Tags:

java

swing

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

like image 375
richs Avatar asked Jun 02 '09 21:06

richs


People also ask

What is the difference between JFrame and JDialog?

JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).

What is Dialogbox in Java?

Dialog boxes are graphical components that are usually used to display errors or give some other information to the user. They are part of the three top-level containers that each Java graphical user interface (GUI) application must have as a root. Dialogs are created as part of a frame.

What is dialog box in swing?

In Java Swing, we can create two kinds of dialogs: standard dialogs and custom dialogs. Custom dialogs are created by programmers. They are based on the JDialog class. Standard dialogs are predefined dialogs available in the Swing toolkit, for example the JColorChooser or the JFileChooser .

What is modal in JDialog?

JDialog(Dialog owner, boolean modal) Creates a dialog box with the specified Dialog owner and modality. JDialog(Dialog owner, String title) Creates a modeless dialog box with the specified Dialog owner and title.


2 Answers

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.

like image 145
Huxi Avatar answered Sep 23 '22 04:09

Huxi


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.

like image 26
thedude19 Avatar answered Sep 23 '22 04:09

thedude19