java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JOptionPane
All Implemented Interfaces:
Accessible, HasGetTransferHandler, Serializable, MenuContainer, ImageObserver
Direct Known Subclasses:
ProgressOptionPane
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
Componentthat is to be the parent of this dialog box. It is used in two ways: theFramethat contains it is used as theFrameparent 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 defaultFrameis 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
StringorStringconstant. 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
Componentis displayed in the dialog.- Icon
- The
Iconis wrapped in aJLabeland displayed in the dialog.- others
- The object is converted to a
Stringby calling itstoStringmethod. The result is wrapped in aJLabeland 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_MESSAGEINFORMATION_MESSAGEWARNING_MESSAGEQUESTION_MESSAGEPLAIN_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_OPTIONYES_NO_OPTIONYES_NO_CANCEL_OPTIONOK_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
Strings. 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
JButtonis created with this as its label.- other
- The
Objectis converted to a string using itstoStringmethod 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
messageTypeparameter.- 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 JavaBeansTM
has been added to the java.beans package.
Please see java.beans.XMLEncoder .
attribute - : isContainer true
description: A component which implements standard dialog box controls.James - GoslingScott - Violet| Nested Class Summary: | ||
|---|---|---|
| protected class | JOptionPane.AccessibleJOptionPane | This class implements accessibility support for the
JOptionPane class. It provides an implementation of the
Java Accessibility API appropriate to option pane user-interface
elements.
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 JavaBeansTM
has been added to the |
| Field Summary | ||
|---|---|---|
| public static final Object | UNINITIALIZED_VALUE | Indicates that the user has not yet selected a value. |
| public static final int | DEFAULT_OPTION | Type meaning Look and Feel should not supply any options -- only
use the options from the JOptionPane. |
| public static final int | YES_NO_OPTION | Type used for showConfirmDialog. |
| public static final int | YES_NO_CANCEL_OPTION | Type used for showConfirmDialog. |
| public static final int | OK_CANCEL_OPTION | Type used for showConfirmDialog. |
| public static final int | YES_OPTION | Return value from class method if YES is chosen. |
| public static final int | NO_OPTION | Return value from class method if NO is chosen. |
| public static final int | CANCEL_OPTION | Return value from class method if CANCEL is chosen. |
| public static final int | OK_OPTION | Return value form class method if OK is chosen. |
| public static final int | CLOSED_OPTION | Return value from class method if user closes window without selecting
anything, more than likely this should be treated as either a
CANCEL_OPTION or NO_OPTION. |
| public static final int | ERROR_MESSAGE | Used for error messages. |
| public static final int | INFORMATION_MESSAGE | Used for information messages. |
| public static final int | WARNING_MESSAGE | Used for warning messages. |
| public static final int | QUESTION_MESSAGE | Used for questions. |
| public static final int | PLAIN_MESSAGE | No icon is used. |
| public static final String | ICON_PROPERTY | Bound property name for icon. |
| public static final String | MESSAGE_PROPERTY | Bound property name for message. |
| public static final String | VALUE_PROPERTY | Bound property name for value. |
| public static final String | OPTIONS_PROPERTY | Bound property name for option. |
| public static final String | INITIAL_VALUE_PROPERTY | Bound property name for initialValue. |
| public static final String | MESSAGE_TYPE_PROPERTY | Bound property name for type. |
| public static final String | OPTION_TYPE_PROPERTY | Bound property name for optionType. |
| public static final String | SELECTION_VALUES_PROPERTY | Bound property name for selectionValues. |
| public static final String | INITIAL_SELECTION_VALUE_PROPERTY | Bound property name for initialSelectionValue. |
| public static final String | INPUT_VALUE_PROPERTY | Bound property name for inputValue. |
| public static final String | WANTS_INPUT_PROPERTY | Bound property name for wantsInput. |
| protected transient Icon | icon | Icon used in pane. |
| protected transient Object | message | Message to display. |
| protected transient Object[] | options | Options to display to the user. |
| protected transient Object | initialValue | Value that should be initially selected in options. |
| protected int | messageType | Message type. |
| protected int | optionType | Option type, one of DEFAULT_OPTION,
YES_NO_OPTION,
YES_NO_CANCEL_OPTION or
OK_CANCEL_OPTION. |
| protected transient Object | value | Currently selected value, will be a valid option, or
UNINITIALIZED_VALUE or null. |
| protected transient Object[] | selectionValues | Array of values the user can choose from. Look and feel will provide the UI component to choose this from. |
| protected transient Object | inputValue | Value the user has input. |
| protected transient Object | initialSelectionValue | Initial value to select in selectionValues. |
| protected boolean | wantsInput | If true, a UI widget will be provided to the user to get input. |
| Fields inherited from java.awt.Container: |
|---|
| ncomponents, component, layoutMgr, containerListener, listeningChildren, listeningBoundsChildren, descendantsCount, INCLUDE_SELF, SEARCH_HEAVYWEIGHTS, modalComp, modalAppContext |
| Constructor: |
|---|
JOptionPane with a test message. |
JOptionPane to display a
message using the
plain-message message type and the default options delivered by
the UI.
|
JOptionPane to display a message
with the specified message type and the default options,
|
JOptionPane to display a message
with the specified message type and options.
|
JOptionPane to display a message
with the specified message type, options, and icon.
|
JOptionPane to display a message
with the specified message type, icon, and options.
None of the options is initially selected.
The options objects should contain either instances of
|
JOptionPane to display a message
with the specified message type, icon, and options, with the
initially-selected option specified.
|
| Methods from java.lang.Object: |
|---|
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from javax.swing.JOptionPane Detail: |
|---|
JDialog
with the specified title.
The returned JDialog will not be resizable by the
user, however programs can invoke setResizable on
the JDialog instance to change this property.
The returned JDialog will be set up such that
once it is closed, or the user clicks on one of the buttons,
the optionpane's value property will be set accordingly and
the dialog will be closed. Each time the dialog is made visible,
it will reset the option pane's value property to
JOptionPane.UNINITIALIZED_VALUE to ensure the
user's subsequent action closes the dialog properly. |
JDialog wrapping
this centered on the parentComponent
in the parentComponent's frame.
title is the title of the returned dialog.
The returned JDialog will not be resizable by the
user, however programs can invoke setResizable on
the JDialog instance to change this property.
The returned JDialog will be set up such that
once it is closed, or the user clicks on one of the buttons,
the optionpane's value property will be set accordingly and
the dialog will be closed. Each time the dialog is made visible,
it will reset the option pane's value property to
JOptionPane.UNINITIALIZED_VALUE to ensure the
user's subsequent action closes the dialog properly. |
JInternalFrame.
The internal frame is created with the specified title,
and wrapping the JOptionPane.
The returned JInternalFrame is
added to the JDesktopPane ancestor of
parentComponent, or components
parent if one its ancestors isn't a JDesktopPane,
or if parentComponent
doesn't have a parent then a RuntimeException is thrown. |
AccessibleContext associated with this JOptionPane.
For option panes, the AccessibleContext takes the form of an
AccessibleJOptionPane.
A new AccessibleJOptionPane instance is created if necessary. |
|
Frame. |
|
|
|
wantsInput
is true. |
Integer.MAX_VALUE.
The value can be
changed by overriding this method in a subclass. |
|
|
|
|
Frame to use for the class methods in
which a frame is not provided. |
|
|
|
UNINITIALIZED_VALUE
implies the user has not yet made a choice, null means the
user closed the window with out choosing anything. Otherwise
the returned value will be one of the options defined in this
object. |
wantsInput property. |
Frame or
Dialog. |
JOptionPane.
This method
is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null. |
|
null, the look and feel
does not provide an icon. |
wantsInput is true. |
Component
that has the focus when the pane is initially displayed. |
wantsInput is true. Note that this method
is invoked internally by the option pane (in response to user action)
and should generally not be called by client programs. To set the
input value initially displayed as selected to the user, use
setInitialSelectionValue. |
|
parentComponent. |
|
newOptions is a Component
it is added directly to the pane,
otherwise a button is created for the element. |
Note: It is recommended that rather than using this method you supply a valid parent. |
null value
implies the user can input whatever they wish, usually by means
of a JTextField.
Sets |
|
|
wantsInput property.
If newValue is true, an input component
(such as a text field or combo box) whose parent is
parentComponent is provided to
allow the user to input a value. If getSelectionValues
returns a non-null array, the input value is one of the
objects in that array. Otherwise the input value is whatever
the user inputs.
This is a bound property. |
|
optionType parameter. |
optionType parameter, where the
messageType
parameter determines the icon to display.
The messageType parameter is primarily used to supply
a default icon from the Look and Feel. |
optionType parameter.
The messageType parameter is primarily used to supply
a default icon from the look and feel. |
|