It is not a matter of IF but WHEN things will go wrong in a computer program. Sometimes there are bugs, errors of one form or another. There are also unforeseen use cases. You can never assume a computer program is perfect. Exception-Handling helps us to catch erroneous events and devise means of correcting them. We discuss this topic here since exception-handling can take more code than should be put into the main line of execution. In such cases, a method in an exception-handling class should be called. Exception handling mechanisms allow a program to continue executing, instead of terminating it abruptly, even if an error occurs in the program.
10.7 From the Java Library: JOptionPane
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):
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.
A 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.
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 (Fig. 10.25). The icon is representative of the dialog’s message type. Among the basic types available in JOptionPane are the
following:
To set the dialog to anything other than the default (informational) type,
you can use the following version of showMessageDialog():
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:
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.
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.