JavaHow to Create Dialog Boxes in Java

How to Create Dialog Boxes 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. Their dependence on frames means that when you close a frame, all its associated dialog boxes also close. Similarly, when you iconify a frame, so too, do you iconify its dialogs.

This Java programming tutorial introduces developers to working with dialog boxes in GUI applications.

Read: Tips to Improve Java Performance

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 JDialog, JOptionPane, or ProgressMonitor.

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 standard dialog is as simple as applying one the following methods to JOptionPane: showMessageDialog or 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, the words on the buttons, and even allows you to ask for input in your dialog box.

The simplest option is to use the showMessageDialog method, as shown in the following Java code example:

JOptionPane.showMessageDialog("Message to user.");

Java Dialog boxes

This dialog box can be created using the following code:

import javax.swing.*;
 
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. JDialog is a Swing component, so you need to load the swing package into your code. Otherwise, you will get a compilation error.

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 takes 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 the four options will bring the corresponding icon on your dialog:

  • warning- JOptionPane.WARNING_MESSAGE
  • information – JOptionPane.INFORMATION_MESSAGE
  • question – JOptionPane.QUESTION_MESSAGE
  • error – JOptionPane.ERROR_MESSAGE

Read: How to Use Lists in Your Java GUI Applications

How to Use JDialog in Java

JOptionPane is good for creating standard dialogs. However, if you wish to have more customization at your disposal, then you need to use JDialog.

One good scenario is when you need to create a non-modal dialog. You can only create this using JDialog.

There are two types of dialogs in Java: modal dialogs and non-modal dialogs.

Modal dialogs are the ones in the earlier JOptionPane section. Modal dialogs make all other program windows inactive until the dependent dialog(s) are closed.

A non-modal dialog, on the other hand, allows developers to use 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.*;
class JDialogBox{
   public static void main(String args[]){
      JDialog dialog = new JDialog();
 
      dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
      dialog.setSize(400,400);  
      dialog.setVisible(true);
   }
}

Creating a dialog 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. It uses the following three, of which it shares the first two with JFrame.

  • DO_NOTHING_ON_CLOSE – The dialog does not close, but instead some other action is invoked in its windowClosing() method
  • HIDE_ON_CLOSE – Removes the dialog from the screen, but it can still be displayed
  • DISPOSE_ON_CLOSE – closes the dialog and frees up any resources it has been using

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 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, in case you want to customize your dialogs, then you need to use JDialog instead of JOptionPane.

Read more Java programming tutorials and developer guides.

Latest Posts

Related Stories