java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.JOptionPane
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
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 current thread until the user's interaction is complete.
| icon | message |
| input value | |
| option buttons | |
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_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
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, or
CLOSED_OPTION.
Examples:
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;
For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JOptionPane key assignments.
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. A future release of Swing will provide support for long term persistence.
JInternalFrame, Serialized Form
| Inner Class Summary | |
protected class |
JOptionPane.AccessibleJOptionPane
JOptionPane.AccessibleJOptionPane
|
| Inner classes inherited from class javax.swing.JComponent |
JComponent.AccessibleJComponent |
| Inner classes inherited from class java.awt.Container |
Container.AccessibleAWTContainer |
| Inner classes inherited from class java.awt.Component |
Component.AccessibleAWTComponent |
| Field Summary | |
static int |
CANCEL_OPTION
copy-> CANCEL_OPTION
|
static int |
CLOSED_OPTION
copy-> CLOSED_OPTION
|
static int |
DEFAULT_OPTION
copy-> DEFAULT_OPTION
|
static int |
ERROR_MESSAGE
copy-> ERROR_MESSAGE
|
protected Icon |
icon
copy-> icon
|
static String |
ICON_PROPERTY
copy-> ICON_PROPERTY
|
static int |
INFORMATION_MESSAGE
copy-> INFORMATION_MESSAGE
|
static String |
INITIAL_SELECTION_VALUE_PROPERTY
copy-> INITIAL_SELECTION_VALUE_PROPERTY
|
static String |
INITIAL_VALUE_PROPERTY
copy-> INITIAL_VALUE_PROPERTY
|
protected Object |
initialSelectionValue
copy-> initialSelectionValue
|
protected Object |
initialValue
copy-> initialValue
|
static String |
INPUT_VALUE_PROPERTY
copy-> INPUT_VALUE_PROPERTY
|
protected Object |
inputValue
copy-> inputValue
|
protected Object |
message
copy-> message
|
static String |
MESSAGE_PROPERTY
copy-> MESSAGE_PROPERTY
|
static String |
MESSAGE_TYPE_PROPERTY
copy-> MESSAGE_TYPE_PROPERTY
|
protected int |
messageType
copy-> messageType
|
static int |
NO_OPTION
copy-> NO_OPTION
|
static int |
OK_CANCEL_OPTION
copy-> OK_CANCEL_OPTION
|
static int |
OK_OPTION
copy-> OK_OPTION
|
static String |
OPTION_TYPE_PROPERTY
copy-> OPTION_TYPE_PROPERTY
|
protected Object[] |
options
copy-> options
|
static String |
OPTIONS_PROPERTY
copy-> OPTIONS_PROPERTY
|
protected int |
optionType
copy-> optionType
|
static int |
PLAIN_MESSAGE
copy-> PLAIN_MESSAGE
|
static int |
QUESTION_MESSAGE
copy-> QUESTION_MESSAGE
|
static String |
SELECTION_VALUES_PROPERTY
copy-> SELECTION_VALUES_PROPERTY
|
protected Object[] |
selectionValues
copy-> selectionValues
|
static Object |
UNINITIALIZED_VALUE
copy-> UNINITIALIZED_VALUE
|
protected Object |
value
copy-> value
|
static String |
VALUE_PROPERTY
copy-> VALUE_PROPERTY
|
static String |
WANTS_INPUT_PROPERTY
copy-> WANTS_INPUT_PROPERTY
|
protected boolean |
wantsInput
copy-> wantsInput
|
static int |
WARNING_MESSAGE
copy-> WARNING_MESSAGE
|
static int |
YES_NO_CANCEL_OPTION
copy-> YES_NO_CANCEL_OPTION
|
static int |
YES_NO_OPTION
copy-> YES_NO_OPTION
|
static int |
YES_OPTION
copy-> YES_OPTION
|
| Fields inherited from class javax.swing.JComponent |
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
| Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
| Fields inherited from interface java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| Constructor Summary | |
JOptionPane
copy-> new JOptionPane()copy-> <JOptionPane var> = new JOptionPane();
|
|
JOptionPane
copy-> new JOptionPane( )copy-> <JOptionPane var> = new JOptionPane(<Object message>);
|
|
JOptionPane
copy-> new JOptionPane(, )copy-> <JOptionPane var> = new JOptionPane(<Object message>, <int messageType>);
|
|
JOptionPane
copy-> new JOptionPane(, , )copy-> <JOptionPane var> = new JOptionPane(<Object message>, <int messageType>, <int optionType>);
|
|
JOptionPane
copy-> new JOptionPane(, , , )copy-> <JOptionPane var> = new JOptionPane(<Object message>, <int messageType>, <int optionType>, <Icon icon>);
|
|
JOptionPane
copy-> new JOptionPane(, , , , )copy-> <JOptionPane var> = new JOptionPane(<Object message>, <int messageType>, <int optionType>, <Icon icon>, <Object[] options>);
|
|
JOptionPane
copy-> new JOptionPane(, , , , , )copy-> <JOptionPane var> = new JOptionPane(<Object message>, <int messageType>, <int optionType>, <Icon icon>, <Object[] options>, <Object initialValue>);
|
|
| Method Summary | |
JDialog |
createDialog(Component parentComponent,
String title)
copy-> .createDialog(, )copy-> <JDialog var>=<JOptionPane>.createDialog(<Component parentComponent>, <String title>);
|
JInternalFrame |
createInternalFrame(Component parentComponent,
String title)
copy-> .createInternalFrame(, )copy-> <JInternalFrame var>=<JOptionPane>.createInternalFrame(<Component parentComponent>, <String title>);
|
AccessibleContext |
getAccessibleContext()
copy-> .getAccessibleContext()copy-> <AccessibleContext var>=<JOptionPane>.getAccessibleContext();
|
static JDesktopPane |
getDesktopPaneForComponent(Component parentComponent)
copy-> JOptionPane.getDesktopPaneForComponent( )copy-> <JDesktopPane var>=JOptionPane.getDesktopPaneForComponent(<Component parentComponent>);
|
static Frame |
getFrameForComponent(Component parentComponent)
copy-> JOptionPane.getFrameForComponent( )copy-> <Frame var>=JOptionPane.getFrameForComponent(<Component parentComponent>);
|
Icon |
getIcon()
copy-> .getIcon()copy-> <Icon var>=<JOptionPane>.getIcon();
|
Object |
getInitialSelectionValue()
copy-> .getInitialSelectionValue()copy-> <Object var>=<JOptionPane>.getInitialSelectionValue();
|
Object |
getInitialValue()
copy-> .getInitialValue()copy-> <Object var>=<JOptionPane>.getInitialValue();
|
Object |
getInputValue()
copy-> .getInputValue()copy-> <Object var>=<JOptionPane>.getInputValue();
|
int |
getMaxCharactersPerLineCount()
copy-> .getMaxCharactersPerLineCount()copy-> <int var>=<JOptionPane>.getMaxCharactersPerLineCount();
|
Object |
getMessage()
copy-> .getMessage()copy-> <Object var>=<JOptionPane>.getMessage();
|
int |
getMessageType()
copy-> .getMessageType()copy-> <int var>=<JOptionPane>.getMessageType();
|
Object[] |
getOptions()
copy-> .getOptions()copy-> <Object var>=<JOptionPane>.getOptions();
|
int |
getOptionType()
copy-> .getOptionType()copy-> <int var>=<JOptionPane>.getOptionType();
|
static Frame |
getRootFrame()
copy-> JOptionPane.getRootFrame()copy-> <Frame var>=JOptionPane.getRootFrame();
|
Object[] |
getSelectionValues()
copy-> .getSelectionValues()copy-> <Object var>=<JOptionPane>.getSelectionValues();
|
OptionPaneUI |
getUI()
copy-> .getUI()copy-> <OptionPaneUI var>=<JOptionPane>.getUI();
|
String |
getUIClassID()
copy-> .getUIClassID()copy-> <String var>=<JOptionPane>.getUIClassID();
|
Object |
getValue()
copy-> .getValue()copy-> <Object var>=<JOptionPane>.getValue();
|
boolean |
getWantsInput()
copy-> .getWantsInput()copy-> <boolean var>=<JOptionPane>.getWantsInput();
|
protected String |
paramString()
copy-> .paramString()copy-> <String var>=<JOptionPane>.paramString();
|
void |
selectInitialValue()
copy-> .selectInitialValue()copy-> <JOptionPane>.selectInitialValue();
|
void |
setIcon(Icon newIcon)
copy-> .setIcon( )copy-> <JOptionPane>.setIcon(<Icon newIcon>);
|
void |
setInitialSelectionValue(Object newValue)
copy-> .setInitialSelectionValue( )copy-> <JOptionPane>.setInitialSelectionValue(<Object newValue>);
|
void |
setInitialValue(Object newInitialValue)
copy-> .setInitialValue( )copy-> <JOptionPane>.setInitialValue(<Object newInitialValue>);
|
void |
setInputValue(Object newValue)
copy-> .setInputValue( )copy-> <JOptionPane>.setInputValue(<Object newValue>);
|
void |
setMessage(Object newMessage)
copy-> .setMessage( )copy-> <JOptionPane>.setMessage(<Object newMessage>);
|
void |
setMessageType(int newType)
copy-> .setMessageType( )copy-> <JOptionPane>.setMessageType(<int newType>);
|
void |
setOptions(Object[] newOptions)
copy-> .setOptions( )copy-> <JOptionPane>.setOptions(<Object[] newOptions>);
|
void |
setOptionType(int newType)
copy-> .setOptionType( )copy-> <JOptionPane>.setOptionType(<int newType>);
|
static void |
setRootFrame(Frame newRootFrame)
copy-> JOptionPane.setRootFrame( )copy-> JOptionPane.setRootFrame(<Frame newRootFrame>);
|
void |
setSelectionValues(Object[] newValues)
copy-> .setSelectionValues( )copy-> <JOptionPane>.setSelectionValues(<Object[] newValues>);
|
void |
setUI(OptionPaneUI ui)
copy-> .setUI( )copy-> <JOptionPane>.setUI(<OptionPaneUI ui>);
|
void |
setValue(Object newValue)
copy-> .setValue( )copy-> <JOptionPane>.setValue(<Object newValue>);
|
void |
setWantsInput(boolean newValue)
copy-> .setWantsInput( )copy-> <JOptionPane>.setWantsInput(<boolean newValue>);
|
static int |
showConfirmDialog(Component parentComponent,
Object message)
copy-> JOptionPane.showConfirmDialog(, )copy-> <int var>=JOptionPane.showConfirmDialog(<Component parentComponent>, <Object message>);
|
static int |
showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType)
copy-> JOptionPane.showConfirmDialog(, , , )copy-> <int var>=JOptionPane.showConfirmDialog(<Component parentComponent>, <Object message>, <String title>, <int optionType>);
|
static int |
showConfirmDialog(Component parentComponent,
Object message,
String title,
|