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:
 2 // Addition program that uses JOptionPane for input and output.
 3 import javax.swing.JOptionPane; // program uses JOptionPane
 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" );
15 // convert String inputs to int values for use in a calculation
16 int number1 = Integer.parseInt( firstNumber );
17 int number2 = Integer.parseInt( secondNumber );
19 int sum = number1 + number2; // add numbers
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

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

Figure 11.3. JOptionPane static constants for message dialogs.

(This item is displayed on page 515 in the print version)

Message dialog type




A dialog that indicates an error to the user.


A dialog with an informational message to the user.


A dialog warning the user of a potential problem.


A dialog that poses a question to the user. This dialog normally requires a response, such as clicking a Yes or a No button.


no icon

A dialog that contains a message, but no icon.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look


Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover

Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615 © 2008-2020.
If you may any questions please contact us: