Simple GUI-Based Input/Output with JOptionPane
Simple GUI Based Input Output with JOptionPane
The applications in Chapters 210 display text at the command window and obtain input from the command window. Most applications you use on a daily basis use windows or dialog boxes (also called dialogs) to interact with the user. For example, e-mail programs allow you to type and read messages in a window provided by the e-mail program. Typically, dialog boxes are windows in which programs display important messages to the user or obtain information from the user. Java's JOptionPane class (package javax.swing) provides prepackaged dialog boxes for both input and output. These dialogs are displayed by invoking static JOptionPane methods. Figure 11.2 presents a simple addition application that uses two input dialogs to obtain integers from the user and a message dialog to display the sum of the integers the user enters.
Figure 11.2. JOptionPane input and message dialogs used to input values from the user and display results.
(This item is displayed on page 513 in the print version)
1 // Fig. 11.2: Addition.java 2 // Addition program that uses JOptionPane for input and output. 3 import javax.swing.JOptionPane; // program uses JOptionPane 4 5 public class Addition 6 { 7 public static void main( String args[] ) 8 { 9 // obtain user input from JOptionPane input dialogs 10 String firstNumber = 11 JOptionPane.showInputDialog( "Enter first integer" ); 12 String secondNumber = 13 JOptionPane.showInputDialog( "Enter second integer" ); 14 15 // convert String inputs to int values for use in a calculation 16 int number1 = Integer.parseInt( firstNumber ); 17 int number2 = Integer.parseInt( secondNumber ); 18 19 int sum = number1 + number2; // add numbers 20 21 // display result in a JOptionPane message dialog 22 JOptionPane.showMessageDialog( null, "The sum is " + sum, 23 "Sum of Two Integers", JOptionPane.PLAIN_MESSAGE ); 24 } // end method main 25 } // end class Addition
|
Line 3 imports class JOptionPane for use in this application. Lines 1011 declare the local String variable firstNumber and assign it the result of the call JOptionPane static method showInputDialog. This method displays an input dialog (see the first screen capture in Fig. 11.2), using the method's String argument ("Enter first integer") as a prompt to the user.
Look-and-Feel Observation 11.2
The prompt in an input dialog typically uses sentence-style capitalizationa style that capitalizes only the first letter of the first word in the text unless the word is a proper noun (for example, Deitel). |
The user types characters in the text field, then clicks the OK button or presses the Enter key to submit the String to the program. Clicking OK also dismisses (hides) the dialog. [Note: If you type in the text field and nothing appears, activate the text field by clicking it with the mouse.] Unlike Scanner, which can be used to input values of several types from the user at the keyboard, an input dialog can input only input Strings. This is typical of most GUI components. Technically, the user can type anything in the input dialog's text field. Our program assumes that the user enters a valid integer value. If the user clicks the Cancel button, showInputDialog returns null. If the user either types a noninteger value or clicks the Cancel button in the input dialog, a runtime logic error will occur in this program and it will not operate correctly. Chapter 13, Exception Handling, discusses how to handle such errors. Lines 1213 display another input dialog that prompts the user to enter the second integer.
To perform the calculation in this application, we must convert the Strings that the user entered to int values. Recall from Section 7.12 that the Integer class's static method parseInt converts its String argument to an int value. Lines 1617 assign the converted values to local variables number1 and number2. Then, line 19 sums these values and assigns the result to local variable sum.
Lines 2223 use JOptionPane static method showMessageDialog to display a message dialog (the last screen capture of Fig. 11.2) containing the sum. The first argument helps the Java application determine where to position the dialog box. The value null indicates that the dialog should appear in the center of the computer screen. The first argument can also be used to specify that the dialog should appear centered over a particular window, which we will demonstrate later in Section 11.8. The second argument is the message to displayin this case, the result of concatenating the String "The sum is " and the value of sum. The third argument"Sum of Two Integers"represents the string that should appear in the dialog's title bar at the top of the dialog. The fourth argumentJOptionPane.PLAIN_MESSAGEis the type of message dialog to display. A PLAIN_MESSAGE dialog does not display an icon to the left of the message. Class JOptionPane provides several overloaded versions of methods showInputDialog and showMessageDialog, as well as methods that display other dialog types. For complete information on class JOptionPane, visit java.sun.com/j2se/1.5.0/docs/api/javax/swing/JOptionPane.html.
Look-and-Feel Observation 11.3
The title bar of a window typically uses book-title capitalizationa style that capitalizes the first letter of each significant word in the text and does not end with any punctuation (for example, Capitalization in a Book Title). |
JOptionPane Message Dialog Constants
The constants that represent the message dialog types are shown in Fig. 11.3. All message dialog types except PLAIN_MESSAGE display an icon to the left of the message. These icons provide a visual indication of the message's importance to the user. Note that a QUESTION_MESSAGE icon is the default icon for an input dialog box (see Fig. 11.2).
Message dialog type |
Icon |
Description |
---|---|---|
ERROR_MESSAGE |
|
A dialog that indicates an error to the user. |
INFORMATION_MESSAGE |
|
A dialog with an informational message to the user. |
WARNING_MESSAGE |
|
A dialog warning the user of a potential problem. |
QUESTION_MESSAGE |
|
A dialog that poses a question to the user. This dialog normally requires a response, such as clicking a Yes or a No button. |
PLAIN_MESSAGE |
no icon |
A dialog that contains a message, but no icon. |