All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----com.sun.java.swing.JComponent
|
+----com.sun.java.swing.JOptionPane
JOptionPane provides numerous class methods for showing simple
message, option type panels and even input panels. To show a message
dialog refer to
the class methods showMessageDialog or
showInternalMessageDialog. To show a dialog with
yes/no or yes/no/cancel choices refer to the class methods
showConfirmDialog or showInternalConfirmDialog.
To bring up a dialog with custom options refer to the class methods
showOptionDialog or showInternalOptionDialog.
To bring up an input panel refer to showInputDialog
or showInternalInputDialog.
(The showInternal... versions of the message are used when
constructing applications with their own desktops, using InternalFrames.)
When the selection is changed setValue will be messaged,
which will generate 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.
Use the class methods showConfirmDialog or
showInternalConfirmDialog to bring up a modal dialog
returning the selected value. (A modal dialog disables the rest
of the application until the user makes a choice.)
Examples:
Show an error dialog with the message alert.
JOptionPane.showMessageDialog(null, "alert", "alert", ERROR_MESSAGE);Show an internal information dialog with the message information.
JOptionPane.showInternalMessageDialog(frame, INFORMATION_MESSAGE, "information", "information");Show an information panel with the options yes/no and message 'choose one'.
JOptionPane.showConfirmDialog(null, "choose one", "choose one", YES_NO);Show an internal information dialog with the options yes/no/cancel and message 'please choose one' and title information.
JOptionPane.showInternalConfirmDialog(frame, "please choose one", "information", YES_NO_CANCEL, INFORMATION_MESSAGE);Show a warning dialog with the options OK, CANCEL, title 'Warning', and message 'Click OK to continue'
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
DEFAULT_OPTION, WARNING_MESSAGE, null, options, options[0]);
Show a dialog asking the user for input
String inputValue = JOptionPane.showInputDialog("Please input a value");
Show a dialog asking for a selection
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionDialog.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_DIALOG, null, possibleValues, possibleValues[0]);
message.
message with the message type messageType.
message, with the message type messageType
and options type optionType.
message, with the message type messageType
option type optionType, and Icon icon.
message, with the message type messageType
option type optionType, Icon icon and
options options.
message, with the message type messageType
option type optionType, Icon icon and
options options and the first value to select
initialValue.
frame
centered around c.
getSelectionValues.
getWantsInput
return true.
getSelectionValues to
newValue.
newValue.
message to newMessage.
newType.
newType.
newValues.
ui that will provide
the current look and feel.
newValue is true, a component will be provided to
allow the user to input a value.
optionType parameter.
optionType parameter, where the messageType
parameter determines the icon to display.
optionType parameter.
component
component with the dialog having the title
title and message type messageType.
message, with the dialog pane parented to
component.
optionType.
icon is used to indicate the default icon to display.
message parented to the Component component.
message, with the message type messageType.
message, with the message type messageType.
message with
options options selecting initialValue
initialValue.
initialValue parameter and
the number of choices is determined by the optionType
parameter.
public static final Object UNITIALIZED_VALUE
public static final int DEFAULT_OPTION
public static final int YES_NO_OPTION
public static final int YES_NO_CANCEL_OPTION
public static final int OK_CANCEL_OPTION
public static final int YES_OPTION
public static final int NO_OPTION
public static final int CANCEL_OPTION
public static final int OK_OPTION
public static final int CLOSED_OPTION
public static final int ERROR_MESSAGE
public static final int INFORMATION_MESSAGE
public static final int WARNING_MESSAGE
public static final int QUESTION_MESSAGE
public static final int PLAIN_MESSAGE
public static final String ICON_PROPERTY
public static final String MESSAGE_PROPERTY
public static final String VALUE_PROPERTY
public static final String OPTIONS_PROPERTY
public static final String INITIAL_VALUE_PROPERTY
public static final String MESSAGE_TYPE_PROPERTY
public static final String OPTION_TYPE_PROPERTY
public static final String SELECTION_VALUES_PROPERTY
public static final String INITIAL_SELECTION_VALUE_PROPERTY
public static final String INPUT_VALUE_PROPERTY
public static final String WANTS_INPUT_PROPERTY
protected static Frame sharedFrame
protected transient Icon icon
protected transient Object message
protected transient Object options[]
protected transient Object initialValue
protected int messageType
protected int optionType
protected transient Object value
protected transient Object selectionValues[]
protected transient Object inputValue
protected transient Object initialSelectionValue
protected boolean wantsInput
public JOptionPane()
public JOptionPane(Object message)
message. message is used as the main
body of the JOptionPane. If message is an instance
of String or Icon it will be placed in a JLabel. If message
is a Component it will be added directly to the body,
and if message is an array each element will be tested
for the above. The message type used is PLAIN_MESSAGE.
public JOptionPane(Object message,
int messageType)
message with the message type messageType.
This uses an option type of DEFAULT_OPTION.
public JOptionPane(Object message,
int messageType,
int optionType)
message, with the message type messageType
and options type optionType.
public JOptionPane(Object message,
int messageType,
int optionType,
Icon icon)
message, with the message type messageType
option type optionType, and Icon icon.
public JOptionPane(Object message,
int messageType,
int optionType,
Icon icon,
Object options[])
message, with the message type messageType
option type optionType, Icon icon and
options options. options should contain
either instances of Components, which will be added directly, or
Strings which will be wrapped in a JButton. If you provide your own
Components in options you must insure that when the
Component is clicked on it messages setValue in the
returned JOptionPane.
public JOptionPane(Object message,
int messageType,
int optionType,
Icon icon,
Object options[],
Object initialValue)
message, with the message type messageType
option type optionType, Icon icon and
options options and the first value to select
initialValue.
public static String showInputDialog(Object message)
public static String showInputDialog(Component component,
Object message)
component
public static String showInputDialog(Component component,
Object message,
String title,
int messageType)
component with the dialog having the title
title and message type messageType.
public static Object showInputDialog(Component component,
Object message,
String title,
int messageType,
Icon icon,
Object selectionValues[],
Object initialSelectionValue)
message will be displayed, the dialog will have the
title title, message type messageType,
and icon icon. The user will able to choose from
selectionValuse, null implies the user can input
what ever they wish, usually by means of a JTextField and
initialSelectionValue is the initial value to prompt
the user with. It is up to the UI to decide how best to represent
the selectionValues, but will usually be one of a
JComboBox, JList or JTextField.
public static void showMessageDialog(Component component,
Object message)
toString method.
public static void showMessageDialog(Component component,
Object message,
String title,
int messageType)
toString method.
public static void showMessageDialog(Component component,
Object message,
String title,
int messageType,
Icon icon)
toString method.
public static int showConfirmDialog(Component component,
Object message)
toString method.
public static int showConfirmDialog(Component component,
Object message,
String title,
int optionType)
optionType parameter.
The option-sets are:
toString method.
public static int showConfirmDialog(Component component,
Object message,
String title,
int optionType,
int messageType)
optionType parameter, where the messageType
parameter determines the icon to display.
The option-sets are:
messageType parameter is primarily used to supply
a default icon from the look and feel.
toString method.
public static int showConfirmDialog(Component component,
Object message,
String title,
int optionType,
int messageType,
Icon icon)
optionType parameter.
The option-sets are:
messageType parameter is primarily used to supply
a default icon from the look and feel.
toString method.
public static int showOptionDialog(Component component,
Object message,
String title,
int optionType,
int messageType,
Icon icon,
Object options[],
Object initialValue)
initialValue parameter and
the number of choices is determined by the optionType
parameter.
The option-sets are:
optionType is YES_NO_OPTION, or YES_NO_CANCEL_OPTION
and the options parameter is null, then the options are
supplied by the look and feel.
The messageType parameter is primarily used to supply
a default icon from the look and feel.
toString method.
toString methods.
If this parameter is null, the options are determined
by the look and feel.
public static JDialog createDialog(Component c,
String title,
JOptionPane pane)
frame
centered around c. If newSize is not null, the
Dialog is set to that size, otherwise the size is not set.
title will be the title of the returned dialog.
The returned JDialog will be set up such that once it is closed,
or the user clicks on the OK button, the dialog will be disposed
and closed.
public static void showInternalMessageDialog(Component component,
Object message)
message parented to the Component component.
The title of the dialog will be Confirm.
public static void showInternalMessageDialog(Component component,
Object message,
String title,
int messageType)
message, with the message type messageType.
The dialog will have title title. component is
used to determine the Frame to create the
Dialog with, if component is null or has no Frame a
default Frame will be used.
The icon will be determined from the messageType.
public static void showInternalMessageDialog(Component component,
Object message,
String title,
int messageType,
Icon icon)
message, with the message type messageType.
The dialog will have title title. component is
used to determine the Frame to create the
Dialog with, if component is null or has no Frame a
default Frame will be used.
public static int showInternalConfirmDialog(Component component,
Object message)
message, with the dialog pane parented to
component. The options will be yes, no and cancel.
public static int showInternalConfirmDialog(Component component,
Object message,
String title,
int optionType)
optionType. This
method will block until the user clicks on one of the buttons or closes
the window. title will be the title of the Dialog.
The Component component is used to determine the Frame
to create the Dialog with, if component is null or
has no Frame a default Frame will be used.
public static int showInternalConfirmDialog(Component component,
Object message,
String title,
int optionType,
int messageType)
messageType
indicates the type of message this, and is primarily used to supply
a default icon from the look and feel.
public static int showInternalConfirmDialog(Component component,
Object message,
String title,
int optionType,
int messageType,
Icon icon)
icon is used to indicate the default icon to display.
public static int showInternalOptionDialog(Component component,
Object message,
String title,
int optionType,
int messageType,
Icon icon,
Object options[],
Object initialValue)
message with
options options selecting initialValue
initialValue. component is used to
determine the frame for the Dialog. If component is null
or does not contain a parent Frame, the root Frame from JOptionPane will
be used. This method blocks until the user closes the dialog,
or clicks on one of the options. The return value will indicate
the index of the chosen option, or CLOSED_OPTION if the user closed
the Dialog. title will be the title of the Dialog. If
optionType is YES_NO_OPTION, YES_NO_CANCEL_OPTION or
OK_CANCEL_OPTION and
options is null the options will be supplied from
the look and feel. messageType is primarily used to
obtain a default image.
public static String showInternalInputDialog(Component component,
Object message)
public static String showInternalInputDialog(Component component,
Object message,
String title,
int messageType)
public static Object showInternalInputDialog(Component component,
Object message,
String title,
int messageType,
Icon icon,
Object selectionValues[],
Object initialSelectionValue)
message will be displayed, the dialog will have the
title title, message type messageType,
and icon icon. The user will able to choose from
selectionValuse, null implies the user can input
what ever they wish, usually by means of a JTextField and
initialSelectionValue is the initial value to prompt
the user with. It is up to the UI to decide how best to represent
the selectionValues, but will usually be one of a
JComboBox, JList or JTextField.
public static JInternalFrame createInternalFrame(Component component,
String title,
JOptionPane pane)
title,
and the JOptionPane pane will be added to the
returned JInternalFrame instance. The returned JInternalFrame will
be add to the JDesktopPane ancestor of component, or components
parent if one its ancestors isn't a JDesktopPane, or if component
doesn't have a parent and RuntimeException will be thrown.
public static Frame getFrameForComponent(Component component)
public static JDesktopPane getDesktopPaneForComponent(Component c)
public static void setRootFrame(Frame newRootFrame)
public static Frame getRootFrame()
public void setUI(OptionPaneUI ui)
ui that will provide
the current look and feel.
public OptionPaneUI getUI()
public void updateUI()
public String getUIClassID()
public void setMessage(Object newMessage)
message to newMessage. Refer to the constructor
for possible values of newMessage.
This is a JavaBeans bound property.
public Object getMessage()
public void setIcon(Icon newIcon)
This is a JavaBeans bound property.
public Icon getIcon()
public void setValue(Object newValue)
This is a JavaBeans bound property.
public Object getValue()
public void setOptions(Object newOptions[])
This is a JavaBeans bound property.
public Object[] getOptions()
public void setInitialValue(Object newInitialValue)
This is a JavaBeans bound property.
public Object getInitialValue()
public void setMessageType(int newType)
newType.
newType should be one of ERROR_MESSAGE,
INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE or
PLAIN_MESSAGE, otherwise an exception will be thrown.
The message type is used by the look and feel to determine the icon to display (if not supplied) as well as potentially how to lay out the component.
This is a JavaBeans bound property.
public int getMessageType()
public void setOptionType(int newType)
newType. newType
must be one of DEFAULT_OPTION, YES_NO_OPTION,
YES_NO_CANCEL_OPTION or OK_CANCEL_OPTION If
newType is not one of these an exception will be
thrown. The option type is used by the look and feel to
determine what buttons to show (unless options are supplied).
This is a JavaBeans bound property.
public int getOptionType()
public void setSelectionValues(Object newValues[])
newValues. If
newValues are non-null this will message
setWantsInput with true. If true, the UI will
provide a UI widget for choosing one of the values in
newValues. Use
setInitialSelectionValue to set the initial value
to select. After the pane as been enabled, inputValue will be
set to the value the user has selected.This is a bound property.
public Object[] getSelectionValues()
public void setInitialSelectionValue(Object newValue)
getSelectionValues to
newValue. This is only used if getWantsInput
is true.
This is a bound property.
public Object getInitialSelectionValue()
getSelectionValues.
public void setInputValue(Object newValue)
newValue.
public Object getInputValue()
getWantsInput
return true.
public void setWantsInput(boolean newValue)
newValue is true, a component will be provided to
allow the user to input a value. If getSelectionValues
returns a non-null the input value will be one from that array,
otherwise the input value will be whatever the user inputs.
This is a bound property.
public boolean getWantsInput()
public void selectInitialValue()
All Packages Class Hierarchy This Package Previous Next Index