Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2022E

Section 10.7 From the Java Library: JOptionPane

A dialog box is a window that can be opened by a program to communicate in some way with the user. Dialog boxes come in many varieties and have many uses in a GUI environment. You’ve undoubtedly encountered them when using your own computer.
For example, a file dialog is opened whenever you want to open or save a file. It provides an interface that lets you name the file (https://docs.oracle.com/javase/7/docs/api/java/awt/FileDialog.html) and helps you search through the computer’s directory structure to find a file.
A warning dialog or error dialog is opened whenever a program needs to notify or warn you that some kind of error occurred. It usually presents an error message and an OK button that you click to dismiss the dialog.
Dialogs are easy to create and use in Java. The Swing component set provides several different kinds of basic dialogs that can be incorporated into your program with one or two lines of code. For example, the IntFieldTester class makes use of a simple message dialog to report an input error to the user. This dialog was created by the following code segment in the program (see Figure 10.6.5):
catch (NumberFormatException e) {
  JOptionPane.showMessageDialog(this,
    "The input must be an integer.  Please reenter.");
  }
This method call displays the window shown in Figure 10.7.1. It contains the error message and an OK button that is used to close the window. The showMessageDialog() method is a static method of the javax.swing.JOptionPane class. This class provides a collection of similar methods for creating and displaying basic dialog boxes.
Figure 10.7.1. Dialog Window
A dialog differs from other kinds of top-level windows—such as JApplet and JFrame—in that it is associated with another window (Figure 10.7.1). The first parameter in this version of the showMessageDialog() method is a reference to the dialog’s parent window. The second parameter is a String representing the message.
The basic message dialog used in this example is known as a modal dialog. This means that once it’s been displayed, you can’t do anything else until you click the OK button and dismiss the dialog. It’s also possible to create nonmodal dialogs. These can stay around on the screen while you move on to other tasks.
Note that the dialog box also contains an icon that symbolizes the purpose of the message (Figure 10.7.2). The icon is representative of the dialog’s message type.
Figure 10.7.2. An error dialog.
Among the basic message types available for the icon in JOptionPane are the following:
JOptionPane.PLAIN_MESSAGE
JOptionPane.INFORMATIONAL_MESSAGE     // Default
JOptionPane.WARNING_MESSAGE
JOptionPane.QUESTION_MESSAGE
JOptionPane.ERROR_MESSAGE
To set the dialog to anything other than the default (informational) type, you can use the following version of showMessageDialog():
showMessageDialog(Component comp, Object message, String title, int msgType);
The first parameter is a reference to the parent window. The second is the message string. The third is a string used as the dialog window’s title, and the fourth is one of the five dialog types. For example, we can change our dialog to an error dialog with the following statement:
catch (IntOutOfRangeException e) {
    JOptionPane.showMessageDialog(this,
            e.getMessage(),
            "Error dialog",
            JOptionPane.ERROR_MESSAGE);
          }
This would produce the dialog shown in Figure 10.7.2.
The other kinds of basic dialogs provided by the JOptionPane class are listed in Table 10.7.3. All of the dialogs listed there can be created with a line or two of code. In addition to these, it’s also possible to create sophisticated dialogs that can be as customized as any other GUI interface you can build in Java.
Table 10.7.3. Basic dialogs provided by JOptionPane.
Dialog Description
Message Dialog Presents a simple error or informational message
Confirm Dialog Prompts the user to confirm a particular action
Option Dialog Lets the user choose from some options
Input Dialog Prompts and inputs a string
In this chapter, you have learned how to handle exceptional conditions that occur in programs. You now know that Java has a default exception handler that can take of many situations, and you also understand that proper program design using Java excpetion-handling elements helps deal with many other situations. This chapter continues the emphasis on good program design for creating useful, stable programs.
You have attempted of activities on this page.