Dialog boxes are components of GUI (graphic user interface). They handle notifications, show user warnings and error messages, and take user input. They are part of the graphic user interface which enhances the user experience. There are multiple types of dialog boxes, each for a different purpose. You can use the JOptionPane class to easily create dialog boxes in Java Swing.

JOptionPane class

JOptionPane class is a built-in class that is used to create dialog boxes in a Java Swing application. It inherits the JComponent class. There are three common yet different types of dialog boxes used in Swing applications: input dialog boxmessage dialog box, and confirm dialog box. The JOptionPane class uses showMessageDialog()showInputDialog() and showConfirmDialog() methods to develop dialog boxes. Dialog boxes are associated with their respective frames. When the frame is destroyed the corresponding dialog box is also destroyed.

The JOptionPane class provides you with four constants to configure your dialog box. They are referred to as Message Types and have their respective icons. They can help you make your dialog box more interactive and easy to understand. Here are these types:

  • Warning — JOptionPane.WARNING_MESSAGE
  • Information — JOptionPane.INFORMATION_MESSAGE
  • Question — JOptionPane.QUESTION_MESSAGE
  • Error — JOptionPane.ERROR_MESSAGE

If you do not wish to display any icon you may use JOptionPane.PLAIN_MESSAGE.

Let’s look at the three methods to create dialog boxes in action.

Creating dialog boxes

The JOptionPane class provides you with the following methods to create different types of dialog boxes:-

  • showMessageDialog() — displays an information message to the user. The method can have multiple combinations of parameters to customize the dialog box.
  • showInputDialog() — displays a dialog that can take input from the user and returns the input as a string.
  • showConfirmDialog() — shows a dialog box for confirmation with different combinations of options.

With the help of JOptionPane, you can develop customized dialog boxes by calling the same method with either additional or fewer parameters.

Let’s look at a simple example:-

package thispackage;

import javax.swing.*;

public class DialogBox {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(
                null,
                "Welcome to Java Swing.",
                "Learning Dialog Boxes",
                JOptionPane.INFORMATION_MESSAGE);
    }
}

You may have noticed that the first argument for our method is null. This doesn’t give us any information as to what the first argument is supposed to do. The argument to be passed is a parent component. The parent component might be a frame or a GUI component. When passing a parent component, the subsequent dialog box will be centered on it. If you pass null instead of a component, the dialog will be centered on the screen instead. Selecting the appropriate parent component might help you enhance and control the flow of your GUI application. The second argument is the message of the dialog box and the third argument is the title of the dialog box.

Let’s look at a dialog box that you can use to take input from the user.

package thispackage;

import javax.swing.*;

public class DialogBox {
    public static void main(String[] args) {
        String name = JOptionPane.showInputDialog(
                null,
                "Enter your name",
                "Input Box",
                JOptionPane.QUESTION_MESSAGE);
    }
}

You can use the value returned by this method as necessary. In the snippet above, the name variable stores the value returned by the method which is the string entered in the dialog box.

Now let’s turn to showConfirmDialog()

package thispackage;

import javax.swing.*;

public class DialogBox {
    public static void main(String[] args) {
        int optionIndex = JOptionPane.showConfirmDialog(
                null,
                "Are you sure?",
                "Your final choice",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE);
        
        if(optionIndex == JOptionPane.YES_OPTION) {
            System.out.println("The user pressed YES!");
        }
    }
}

The method returns an integer that reflects the user’s choice. The fourth argument to the method determines the options available to the user. As a programmer, you get to use the following configurations:-

  • DEFAULT_OPTION — A single OK option.
  • YES_NO_OPTION — Two options; Yes and No.
  • YES_NO_CANCEL_OPTION — Three options; YESNOCANCEL.
  • OK_CANCEL_OPTION — Two options; OK and Cancel.

All the methods you have looked at can take multiple parameters. However, you can omit some of those parameters and still create a functional dialog box. It is best to experiment with dialog boxes on your own and see what works and what doesn’t.

Customizing the JOptionPane class with icons

The JOptionPane class not only helps you develop dialog boxes but also gives you the feature to customize them according to your needs. Take a look at the customized dialog box that displays an image and some text.

package thispackage;

import javax.swing.*;

public class DialogBox {
    public static void main(String[] args) {
        ImageIcon icon = new ImageIcon("Hi.jpg");
        JOptionPane.showMessageDialog(
                null,
                "Welcome",
                "Display image",
                JOptionPane.INFORMATION_MESSAGE, 
                icon);
    }
}

Conclusion

In this topic, you worked with dialog boxes in Java Swing. So far,

  • You can create dialog boxes using the JOptionPane class and its methods.
  • You can use showMessageDialog()showInputDialog() or showConfirmDialog() to create the dialog box of your choice.
  • You can use different MessageTypes to make your dialog box interactive and easy to understand.
  • Methods under JOptionPane can also accommodate fewer parameters or different combinations of parameters.
  • You can create dialog boxes using custom icons with the help of the ImageIcon class.

Leave a Reply

Your email address will not be published.