How to Create Dialog Boxes in Java
Follow a simple tutorial for creating dialog boxes in Java using the JOptionPane and JDialog methods, and learn the high-level advantages of each approach.
Join the DZone community and get the full member experience.
Join For FreeDialog boxes are graphical components that are usually used to display errors or give some other information (e.g., request input) to the user. They are commonly part of the three top-level containers that each Java graphical user interface (GUI) application must have as a root. Dialogs are typically associated with a parent frame. Their dependence on frames means that when you close a frame, all its associated dialog boxes also close. However, note that minimizing (iconifying) a frame does not automatically minimize its dialogs in modern Java implementations.
This Java programming tutorial introduces developers to creating and working with dialog boxes in Java GUI applications.
How to Create a Dialog Box in Java
There are several ways in which a developer can create a dialog box in Java. Programmers can use:
JOptionPane
, a quick and simple way to display standard dialogsJDialog
, which offers more customization and controlProgressMonitor
, which is used specifically for progress dialogs
To create a standard dialog, you can simply use the JOptionPane
class. This GUI programming tutorial will mainly focus on using this method. However, if you wish to have more customization over the features of your dialog, then you need to use JDialog
.
Creating a Dialog With JOptionPane
JOptionPane
provides an easy way to create standard dialog boxes. The two most commonly used methods are:
showMessageDialog
showOptionDialog
The showMessageDialog
method creates a basic one-button dialog box. The showOptionDialog
method, however, enables you to customize features — like the number of buttons and the words on the buttons — and it even allows you to ask for input in your dialog box.
The simplest option is to use the showMessageDialog
method, the structure for which is shown in the following Java code example:
JOptionPane.showMessageDialog(parentComponent, message, title, messageType);
In the showMessageDialog
method structure:
parentComponent
is the parent framemessage
is the text that will be displayed in the dialog boxtitle
is the dialog box's titlemessageType
defines the kind of icon that will be shown
With the showMessageDialog
method, you can also customize the icons and title of your dialog. The parameter list is as follows:
showMessageDialogmessage(message, title, icon)
The icon of your dialog box takes on the look and feel of the environment in which the program is being run. There are four icon options that you can use, each of which will add the corresponding icon to your dialog box:
- ⚠️ Warning –
JOptionPane.WARNING_MESSAGE
- ℹ️ Information –
JOptionPane.INFORMATION_MESSAGE
- ❓ Question –
JOptionPane.QUESTION_MESSAGE
- ❌ Error –
JOptionPane.ERROR_MESSAGE
Here is example code for creating a simple dialog box:
import javax.swing.*;
public class SimpleDialog {
public static void main(String[] args){
JFrame frame = new JFrame("Main Window");
JOptionPane.showMessageDialog(frame,
"Message for the dialog box goes here.",
"Error",
JOptionPane.ERROR_MESSAGE);
frame.setSize(350, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Notice that line 1 in the code above imports the Swing package. Note that JDialog
is a Swing component, so you need to load the swing package into your code. Otherwise, you will get a compilation error.
The code example creates a dialog box for an error message, and since JOptionPane.ERROR_MESSAGE
is the message type, it will use a standard error message icon.
Using JDialog for Custom Dialogs
JOptionPane
is ideal for creating standard dialogs. However, if you wish to have more customization and flexibility at your disposal, then you need to use JDialog
. One advantageous scenario is when you need to create a non-modal dialog, which you can only do using JDialog
.
There are two types of dialogs in Java, modal dialogs and non-modal dialogs:
- A modal dialog makes all other program windows inactive — and prevents interaction with other windows — until the dependent dialog(s) are closed. A modal dialog was used in the earlier
JOptionPane
section. - A non-modal dialog allows developers to interact with other top-level windows of the same program while the dialog is still open.
See the Java code example below on how to use JDialog
:
import javax.swing.*;
public class JDialogBox {
public static void main(String[] args) {
JDialog dialog = new JDialog();
dialog.setTitle("Custom Dialog");
dialog.setSize(400, 400);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(false); // Allows interaction with other windows
dialog.setVisible(true);
}
}
Creating a dialog box using JDialog
is similar to using JFrame
to create a frame. However, there are some notable differences. First, you need to create an instance of JDialog()
. Note that there is no title defined in its constructor. After creating an instance, you can set the default close operation.
Unlike JFrame
, JDialog
does not use the EXIT_ON_CLOSE
window closing event. JDialog
uses the following three, the first two of which it shares with JFrame
:
DO_NOTHING_ON_CLOSE
– The dialog does not close, but instead, some other action is invoked in itswindowClosing()
method.HIDE_ON_CLOSE
– The dialog is hidden from the screen, but it is stored in memory.DISPOSE_ON_CLOSE
– The dialog closes and frees up any resources that it has been using (this is the recommended method).
The setsize()
method enables you to size your dialog, and setVisible()
ensures that your dialog actually displays on your screen.
Final Thoughts on Java Dialog Boxes
Dialog boxes in Java enable you to show the user warnings, errors, or even images. They can also be used to show a progress bar for processes being carried out. Remember: JOptionPane
is the best choice for quick implementations, whereas if you want a custom layout, more controls, or a non-modal dialog box, you'll need to use JDialog
.
Opinions expressed by DZone contributors are their own.
Comments