Java Reference
In-Depth Information
CHAPTER 9
■ ■ ■
Pop-Ups and Choosers
I
n Chapter 8, you looked at the top-level containers such as
JFrame
and
JApplet
. In addition,
you explored the
JDialog
class used to create pop-up windows to display messages or get user
input. Although the
JDialog
class works perfectly well, the Swing component set also offers
several simpler approaches to get user input from pop-up windows, which you will explore in
this chapter.
The
JOptionPane
class is useful for displaying messages, obtaining textual user input, or
getting the answer to a question. The
ProgressMonitor
and
ProgressMonitorInputStream
classes enable you to monitor the progress of lengthy tasks. In addition, the
JColorChooser
and
JFileChooser
classes come equipped with feature-filled pop-up windows for getting a color
choice from a user or getting a file or directory name. By using these additional classes, your
user interface development tasks can be accomplished much more quickly and easily.
JOptionPane Class
JOptionPane
is a special class for creating a panel to be placed in a pop-up window. The purpose
of the panel is to display a message to a user and get a response from that user. To accomplish
its task, the panel presents content in four areas (see Figure 9-1):
•
Icon:
The icon area is for the display of an
Icon
to indicate the type of message being
displayed to the user. It's the responsibility of the installed look and feel to provide
default icons for certain types of messages, but you can provide your own if you need
to display another icon type.
•
Message:
The primary purpose of this area is to display a text message. In addition, the
area can contain any optional set of objects to make the message more informational.
•
Input:
The input area allows a user to provide a response to a message. The response can
be free form, in a text field, or from a pick list in a combo box or list control. For yes or no
type questions, the button area should be used instead.
•
Button:
The button area is also for getting user input. Selection of a button in this area
signals the end of the usage of the
JOptionPane
. Default sets of button labels are available,
or you can display any number of buttons, including none, with any labels you desire.
267