Section 10.7. From the Java Library: javax.swing.JOptionPane


[Page 501 (continued)]

10.7. From the Java Library: javax.swing.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 have 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.

java.sun.com/docs


A warning dialog, or error dialog, is opened whenever a program needs to notify or warn you that some kind of error has 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):

catch (NumberFormatException e) {   JOptionPane.showMessageDialog(this,     "The input must be an integer. Please reenter."); } 


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.


[Page 502]

A dialog differs from other kinds of top-level windowssuch as JApplet and JFramein 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.

Figure 10.24. A dialog window cannot stand alone. It must be created by a top-level window.


The basic message dialog used in this example is known as a modal dialog. This means that once it has 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 on the screen while you move on to other tasks.

Modal and nonmodal dialogs


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:

JOptionPane.PLAIN_MESSAGE JOptionPane.INFORMATIONAL_MESSAGE      // Default JOptionPane.WARNING_MESSAGE JOptionPane.QUESTION_MESSAGE JOptionPane.ERROR_MESSAGE 


Figure 10.25. An error dialog.


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); 



[Page 503]

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.25.

The 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 is also possible to create sophisticated dialogs that can be as customized as any other GUI interface you can build in Java.

Table 10.4. 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


Basic Swing dialogs





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net