Java Reference
In-Depth Information
Figure 9-1.
JOptionPane parts
All the areas are optional (although having a panel without at least a message and a button
makes the option pane virtually useless).
Besides being a panel with four sections within a pop-up window, the
JOptionPane
is
capable of automatically placing itself in a pop-up window and managing the acquisition of
the user's response. It can place itself in either a
JDialog
or a
JInternalFrame
, depending on the
type of GUI you're providing to the user. With the help of an
Icon
and set of
JButton
components,
the
JOptionPane
can easily be configured to show a variety of messages and input dialogs.
■
Note
Because the
JOptionPane
can automatically place itself in a
JDialog
, you might never need to
create a
JDialog
directly.
Creating a JOptionPane
You can either manually create a
JOptionPane
through one of its 7 constructors or go through
one of the 25 factory methods discussed later in the chapter, in the “Automatically Creating a
JOptionPane in a Pop-Up Window” section. You have the most control when manually creating the
JOptionPane
. However, you then must place it in a pop-up window, show the window, and
finally manage getting the response.
Because of the ease of use provided by the methods that do everything automatically, you
might think you would
only
use the factory methods when working with
JOptionPane
. However,
throughout this chapter, you'll discover several other reasons why you might want to do things
manually. In addition, when you use a visual-programming environment, the environment
treats the
JOptionPane
as a JavaBean component and will ignore the factory methods.
For the seven constructors, you can have different permutations of six different arguments.
The arguments allow you to configure something in one of the four different areas shown in
Figure 9-1. The six arguments are the message, the message type, an option type, an icon, an
array of options, and an initial option setting. The use of these arguments is shared with the
factory methods.
Let's first look at the seven constructors, and then explore the different arguments. Notice
that the constructor arguments are cascading and only add additional arguments to the
previous constructor.