Java Reference
In-Depth Information
components. The user can type
any
characters in the input dialog's text field. Our program
assumes that the user enters a
valid
integer. If the user clicks
Cancel
,
showInputDialog
returns
null
. If the user either types a noninteger value or clicks the
Cancel
button in the
input dialog, an exception will occur and the program will not operate correctly. Lines 12-
13 display another input dialog that prompts the user to enter the second integer. Each
JOptionPane
dialog that you display is a so called
modal dialog
—while the dialog is on
the screen, the user
cannot
interact with the rest of the application.
Look-and-Feel Observation 12.3
Do not overuse modal dialogs, as they can reduce the usability of your applications. Use a
modal dialog
only
when it's necessary to prevent users from interacting with the rest of an
application until they dismiss the dialog.
Converting
String
s to
int
Values
To perform the calculation, we convert the
String
s that the user entered to
int
values.
Recall that the
Integer
class's
static
method
parseInt
converts its
String
argument to
an
int
value and might throw a
NumberFormatException
. Lines 16-17 assign the convert-
ed values to local variables
number1
and
number2
, and line 19 sums these values.
Message Dialogs
Lines 22-23 use
JOptionPane
static
method
showMessageDialog
to display a message di-
alog (the last screen of Fig. 12.2) containing the sum. The first argument helps the Java ap-
plication determine where to
position
the dialog box. A dialog is typically displayed from a
GUI application with its own window. The first argument refers to that window (known as
the
parent window
) and causes the dialog to appear centered over the parent (as we'll do in
Section 12.9). If the first argument is
null
, the dialog box is displayed at the
center
of your
screen. The second argument is the
message
to display—in this case, the result of concatenat-
ing the
String
"The
sum
is
"
and the value of
sum
. The third argument—
"Sum
of
Two
In-
tegers"
—is the
String
that should appear in the
title bar
at the top of the dialog. The fourth
argument—
JOptionPane.PLAIN_MESSAGE
—is 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, visit
http://
Look-and-Feel Observation 12.4
The title bar of a window typically uses
book-title capitalization
—a style that capital-
izes the first letter of each significant word in the text and does not end with any punctu-
ation (for example, Capitalization in a Book Title).
JOptionPane
Message Dialog Constants
The constants that represent the message dialog types are shown in Fig. 12.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. A
QUESTION_MESSAGE
icon is the
default icon
for an input dialog box (see Fig. 12.2).