JOptionPane
makes it easy to pop up a standard dialog box that
prompts users for a value or informs them of something.
For information about using JOptionPane
, see
How to Make Dialogs,
a section in The Java Tutorial.
While the JOptionPane
class may appear complex because of the large number of methods, almost
all uses of this class are one-line calls to one of the static
showXxxDialog
methods shown below:
Each of these methods also comes in a
Method Name Description showConfirmDialog Asks a confirming question, like yes/no/cancel. showInputDialog Prompt for some input. showMessageDialog Tell the user about something that has happened. showOptionDialog The Grand Unification of the above three.
showInternalXXX
flavor, which uses an internal frame to hold the dialog box (see
JInternalFrame
).
Multiple convenience methods have also been defined -- overloaded
versions of the basic methods that use different parameter lists.
All dialogs are modal. Each showXxxDialog
method blocks
the caller until the user's interaction is complete.
icon | message |
input value | |
option buttons |
ComponentOrientation
property.
Parameters:
The parameters to these methods follow consistent patterns:
- parentComponent
- Defines the
Component
that is to be the parent of this dialog box. It is used in two ways: theFrame
that contains it is used as theFrame
parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may benull
, in which case a defaultFrame
is used as the parent, and the dialog will be centered on the screen (depending on the L&F).- message
- A descriptive message to be placed in the dialog box. In the most common usage, message is just a
String
orString
constant. However, the type of this parameter is actuallyObject
. Its interpretation depends on its type:
- Object[]
- An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive -- each object in the array is interpreted according to its type.
- Component
- The
Component
is displayed in the dialog.- Icon
- The
Icon
is wrapped in aJLabel
and displayed in the dialog.- others
- The object is converted to a
String
by calling itstoString
method. The result is wrapped in aJLabel
and displayed.- messageType
- Defines the style of the message. The Look and Feel manager may lay out the dialog differently depending on this value, and will often provide a default icon. The possible values are:
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
- optionType
- Defines the set of option buttons that appear at the bottom of the dialog box:
You aren't limited to this set of option buttons. You can provide any buttons you want using the options parameter.
DEFAULT_OPTION
YES_NO_OPTION
YES_NO_CANCEL_OPTION
OK_CANCEL_OPTION
- options
- A more detailed description of the set of option buttons that will appear at the bottom of the dialog box. The usual value for the options parameter is an array of
String
s. But the parameter type is an array ofObjects
. A button is created for each object depending on its type:
- Component
- The component is added to the button row directly.
- Icon
- A
JButton
is created with this as its label.- other
- The
Object
is converted to a string using itstoString
method and the result is used to label aJButton
.- icon
- A decorative icon to be placed in the dialog box. A default value for this is determined by the
messageType
parameter.- title
- The title for the dialog box.
- initialValue
- The default selection (input value).
When the selection is changed, setValue
is invoked,
which generates a PropertyChangeEvent
.
If a JOptionPane
has configured to all input
setWantsInput
the bound property JOptionPane.INPUT_VALUE_PROPERTY
can also be listened
to, to determine when the user has input or selected a value.
When one of the showXxxDialog
methods returns an integer,
the possible values are:
YES_OPTION
NO_OPTION
CANCEL_OPTION
OK_OPTION
CLOSED_OPTION
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
JOptionPane.showInternalMessageDialog(frame, "information", "information", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
JOptionPane.showInternalConfirmDialog(frame, "please choose one", "information", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
Object[] options = { "OK", "CANCEL" }; JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
String inputValue = JOptionPane.showInputDialog("Please input a value");
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
JOptionPane
directly, the
standard pattern is roughly as follows:
JOptionPane pane = new JOptionPane(arguments); pane.set.Xxxx(...); // Configure JDialog dialog = pane.createDialog(parentComponent, title); dialog.show(); Object selectedValue = pane.getValue(); if(selectedValue == null) return CLOSED_OPTION; //If there is not an array of option buttons: if(options == null) { if(selectedValue instanceof Integer) return ((Integer)selectedValue).intValue(); return CLOSED_OPTION; } //If there is an array of option buttons: for(int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) { if(options[counter].equals(selectedValue)) return counter; } return CLOSED_OPTION;
Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeans™
has been added to the java.beans
package.
Please see java.beans.XMLEncoder
.
extends
JInternalFrame
@beaninfo
attribute: isContainer true
description: A component which implements standard dialog box controls.