The main function of a dialog box is for an application or website to retrieve some input from the user. That input can be an acknowledgment that they have read a message or something they enter into a text area.

A dialog box immediately captures a user’s attention. It’s a perfect tool for collecting or displaying important information.

Java is a diverse language that provides several classes to create dialog boxes. These classes include JOptionPane, JDialog, and JFrame.

The JOptionPane Class

You can create a standard dialog box using one of several static methods belonging to the JOptionPane class. These include:

  • showMessageDialog(), which relays a message to the user.
  • showConfirmDialog(), which asks a question that requires confirmation.
  • showInputDialog(), which prompts a user for input.
  • showOptionDialog(), which is a combination of the three other methods.

Creating a JOptionPane Dialog Box

        import javax.swing.JFrame;
import javax.swing.JOptionPane;
 
public class JOptionPaneApp {
    JOptionPaneApp() {
        JFrame frame = new JFrame();
        JOptionPane.showMessageDialog(frame, "This is a JOptionPane message window.");
    }
 
    public static void main(String[] args) {
        new JOptionPaneApp();
    }
}

The code above creates the following dialog box:

JOptionPane window

Although JOptionPane provides standard dialog boxes, it has many options allowing you to tweak its behavior. For example, the message dialog can take one of several types. The one above is an example of an INFORMATION_MESSAGE, which is the default. The other message types are:

  • ERROR_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE

Creating an Error Message

Here’s an example of how to use a different message type for your JOptionPane dialog:

        JOptionPane.showMessageDialog(frame, "This is a JOptionPane error message window.",
    "Error", JOptionPane.ERROR_MESSAGE);

Replace the showMessageDialog() line in the original program with the line of code above, and you’ll see the following error dialog:

JOptionPane error message window

The JDialog Class

The JDialog class lets you create custom dialog boxes. This Java class belongs to the javax.swing package and extends the Dialog class. It has access to a wide variety of direct and indirect methods. This Java class has a total of 16 Java constructors.

Apart from the default constructor, each of the 15 others takes a frame, a window, or a dialog with a combination of several other arguments.

The primary JDialog Constructors include:

  • JDialog() creates a dialog box without a frame, title, or mode.
  • JDialog(Dialog owner, String title, boolean modal) creates a dialog box with a Dialog owner, a string title, and a mode.
  • JDialog(Frame owner, String title, boolean modal) creates a dialog box with a Frame owner, a string title, and a mode.
  • JDialog(Window owner, String title, boolean modal) creates a dialog box with a Window owner, a string title, and a mode.

Creating a JDialog Dialog Box

        import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
 
public class JDialogApp {
    JDialogApp() {
        JFrame frame = new JFrame();
        JDialog dialog = new JDialog(frame, true);
        dialog.setLayout(new FlowLayout());
 
        JLabel displayText = new JLabel("This is a JDialog window.");
        JButton btn = new JButton("OK");
       
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        });
 
        dialog.add(displayText);
        dialog.add(btn);
 
        dialog.setSize(200,150);
        dialog.setTitle("Dialog Window");
        dialog.setVisible(true);
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
 
    public static void main(String args[]) {
        new JDialogApp();
    }
}

The code above creates the following dialog box:

JDialog window

There are several important aspects of the code that you should note. The program uses the JDialog(Frame owner, Boolean modal) constructor, to which it passes a JFrame and the “true” value as arguments. The JFrame is the owner of the JDialog, which means that it is responsible for displaying the dialog box. The “true” value means that the dialog blocks input to other related windows when it displays.

The constructor in the code above does not take a title as its argument. However, the frame owner needs a title. So, for that task, you can use the setTitle() method, which is available through the Dialog class that JDialog extends. The other important methods in the code are setVisible(), setSize(), and setDefaultCloseOperation().

setVisible takes a Boolean value and is responsible for displaying the frame. setSize takes the height and width of the dialog window. setDefaultCloseOperation takes one of three values to decide what happens when a user closes the dialog.

The JFrame Class

JDialog and JOptionPane both use the JFrame class to create dialog boxes. However, the JFrame class can create dialog boxes on its own. The JFrame class extends the Frame class and, much like the JDialog class, it allows you to create custom dialog boxes.

JFrame has four constructors and several direct and indirect methods that you will need to use to create a dialog box.

Creating a JFrame Dialog Box

        import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
 
public class JFrameApp {
    JFrameApp() {
        JFrame frame = new JFrame();
        frame.setTitle("Dialog Window");
        JPanel panel = new JPanel();
 
        JLabel displayText = new JLabel("This is a JFrame window.");
        panel.add(displayText);
        JButton btn = new JButton("OK Button");
 
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
            }
        });
 
        panel.add(btn);
 
        frame.add(panel);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(250, 150);
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        new JFrameApp();
    }
}

The code above creates the following dialog box:

JFrame window

The code uses the default JFrame constructor, and several familiar methods, such as the setTitle() method (used in the JDialog app above). An unfamiliar object in the program above is the JPanel, which is a generic container. This gives JFrame the flexibility to add multiple layouts and components to a frame.

The ActionListener() and actionPerformed() methods handle the event of a user clicking the OK button.

Which Java Class Is Best for Creating Dialog Boxes?

The single purpose of the JOptionPane class is to make the dialog box creation process more convenient for Java developers. However, if you need a more custom dialog box the JDialog class is the next best option.

The JFrame class creates UIs, but you can use it to create many different elements of a GUI, including dialog boxes.

The dialog box created with the JFrame class has a minimized icon, while the others do not. If you want a modal dialog box, that the user must action before completing a task, then JFrame is not the best approach.