Exceptions: When Things Go Wrong

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 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.23):

Annotation 2020-03-29 182037

This method call displays the window shown in Figure 10.16. 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.

Annotation 2020-03-29 182219A dialog differs from other kinds of top-level windows—such as JApplet and JFrame—in that it is associated with another window (Fig. 10–24). 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. 

Annotation 2020-03-29 182343Annotation 2020-03-29 182429

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. Annotation 2020-03-29 182536

Note that the dialog box also contains an icon that symbolizes the purpose of the message (Fig. 10.25). The icon is representative of the dialog’s message type. Among the basic types available in JOptionPane are the following:

Annotation 2020-03-29 182720

To set the dialog to anything other than the default (informational) type, you can use the following version of showMessageDialog(): 

Annotation 2020-03-29 182803

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:

Annotation 2020-03-29 182847

This would produce the dialog shown in Figure 10.25. T

he other kinds of basic dialogs provided by the JOptionPane class are listed in Table 10.4. 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.

Annotation 2020-03-29 182948

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 exception-handling elements helps deal with many other situations. This chapter continues the emphasis on good program design for creating useful, stable programs.