JOptionPane makes it easy to pop up a standard dialog box that prompts
the user for information or displays some information.
The labels of the option-buttons displayed within the popup dialog can be
customized by changing static variables in the JOptionPane class.
Similarly, "accelerator keys" can be set to have the same effect
as the option-buttons. For example, the following code would set
the label of the OK button to "OK (F1)", and set the F1 key to
have the same effect as the OK button:
JOptionPane.OK_LABEL = "OK (F1)";
JOptionPane.OK_ACCELERATOR = KeyEvent.VK_F1;
Note that after the buttons have been customized, they stay customized
for all future invocations of the JOptionPane "showXXXDialog()" methods.
The parameters to these methods follow consistent patterns:
parentComponent
parentComponent
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
DEFAULT_OPTION
YES_NO_OPTION
YES_NO_CANCEL_OPTION
OK_CANCEL_OPTION
options
CANCEL_ACCELERATOR
public static int CANCEL_ACCELERATOR
CANCEL_LABEL
public static String CANCEL_LABEL
CANCEL_OPTION
public static final int CANCEL_OPTION
CLOSED_OPTION
public static final int CLOSED_OPTION
DEFAULT_OPTION
public static final int DEFAULT_OPTION
ERROR_MESSAGE
public static final int ERROR_MESSAGE
INFORMATION_MESSAGE
public static final int INFORMATION_MESSAGE
NO_ACCELERATOR
public static int NO_ACCELERATOR
NO_LABEL
public static String NO_LABEL
NO_OPTION
public static final int NO_OPTION
OK_ACCELERATOR
public static int OK_ACCELERATOR
OK_CANCEL_OPTION
public static final int OK_CANCEL_OPTION
OK_LABEL
public static String OK_LABEL
OK_OPTION
public static final int OK_OPTION
PLAIN_MESSAGE
public static final int PLAIN_MESSAGE
QUESTION_MESSAGE
public static final int QUESTION_MESSAGE
WARNING_MESSAGE
public static final int WARNING_MESSAGE
YES_ACCELERATOR
public static int YES_ACCELERATOR
YES_LABEL
public static String YES_LABEL
YES_NO_CANCEL_OPTION
public static final int YES_NO_CANCEL_OPTION
YES_NO_OPTION
public static final int YES_NO_OPTION
YES_OPTION
public static final int YES_OPTION
_initialValue
protected Object _initialValue
Option that should be initially selected in _options
.
_inputValue
protected String _inputValue
_message
protected Object _message
_messagetype
protected int _messagetype
_options
protected Object[] _options
Array of options to display to the user in the bottom button-panel.
The objects in this array can be any combination of Strings or
components which are subclasses of AbstractButton.
Buttons are just added to the bottom button-panel; Strings are
wrapped in a JButton which is then added to the button-panel.
_optiontype
protected int _optiontype
Determines which option buttons to display (unless an array
of options is explicitly specified with setOptions()
).
_value
protected Object _value
The currently selected option.
_wantsInput
protected boolean _wantsInput
If true, an TextField will be displayed for the user to provide
input.
JOptionPane
public JOptionPane()
Creates a JOptionPane with a test message
JOptionPane
public JOptionPane(Object message_)
Creates an instance of JOptionPane to display the specified message.
message_
- the message to display. It can be a String
or an array of Strings. If it is an array of Strings, they are
stacked vertically.
JOptionPane
public JOptionPane(Object message_,
int messagetype_)
Creates an instance of JOptionPane to display the specified message.
message_
- the message to display. It can be a String
or an array of Strings.messagetype_
- is ignored. It is used in javax.swing to
determine which icon to display.
JOptionPane
public JOptionPane(Object message_,
int messagetype_,
int optiontype_)
Creates an instance of JOptionPane to display the specified message.
message_
- the message to display. It can be a String
or an array of Strings.messagetype_
- is ignored. It is used in javax.swing to
determine which icon to display.optiontype_
- determines which option-buttons to display. Allowed
values are: DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, and
OK_CANCEL_OPTION.
JOptionPane
public JOptionPane(Object message_,
int messageType_,
int optionType_,
Object icon_,
Object[] options_,
Object initialValue_)
Creates an instance of JOptionPane to display the specified message.
message_
- the message to display. It can be a String
or an array of Strings.messageType_
- is ignored. It is used in javax.swing to
determine which icon to display.optionType_
- determines which option-buttons to display. Allowed
values are: DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, and
OK_CANCEL_OPTION.icon_
- the Icon image to display (not used by Charva; it is here
for compatibility with Swing).options_
- the choices the user can selectinitialValue_
- the choice that is initially selected; if null,
then nothing will be initially selected; only meaningful if "options"
is used
createDialog
public JDialog createDialog(Component owner_,
String title_)
Creates and returns a new JDialog for displaying the required
message. The dialog inherits the foreground and background colors
of the owner_
component and is centered on it.
If owner_
is null, default (black/white) colors are used.
getInitialValue
public Object getInitialValue()
Returns the initial value that is to be enabled -- the Component
that has the focus when the pane is initially displayed.
(NOT IMPLEMENTED YET).
getInputValue
public Object getInputValue()
Returns the value the user has input, (relevant only if
_wantsInput is true).
getOptions
public Object[] getOptions()
Returns the choices the user can make.
getValue
public Object getValue()
Returns the option the user has selected.
getWantsInput
public boolean getWantsInput()
Returns the value of _wantsInput.
setInitialSelectionValue
public void setInitialSelectionValue(Object value_)
Sets the default input value that is displayed to the user.
Only used if _wantsInput
is true.
setInitialValue
public void setInitialValue(Object initialValue_)
Sets the initial value that is to be enabled -- the Component
that has the focus when the pane is initially displayed.
(NOT IMPLEMENTED YET).
setInputValue
public void setInputValue(Object value_)
Sets the initial value of the text field for the user to modify.
setOptions
public void setOptions(Object[] newOptions_)
Sets the options this pane displays in the button-panel at
the bottom. If an element in
newOptions is an instance of a subclass of AbstractButton (for
example, a JButton), it is added directly to the pane,
otherwise a button is created for the element. The advantage
of adding a button rather than a string is that a mnemonic
can be set for the button.
setWantsInput
public void setWantsInput(boolean newvalue_)
If newvalue_ is true, a JTextField will be displayed for
the user to provide text input.
showConfirmDialog
public static int showConfirmDialog(Component parent_,
Object message_,
String title_,
int optiontype_)
Brings up a dialog where the number of choices is dependent on the
value of the optiontype_ parameter.
parent_
- Determines the frame in which the dialog is displayed.message_
- the String to display.title_
- the title of the dialog.optiontype_
- must be YES_NO_OPTION or YES_NO_CANCEL_OPTION.
showInputDialog
public static String showInputDialog(Component parent_,
Object message_,
String title_,
int messagetype_)
Brings up a dialog that allows the user to input a value.
parent_
- Determines the frame in which the dialog is displayed.message_
- the String to display.title_
- the title of the dialog.messagetype_
- is ignored (it is used in javax.swing to determine
which icon to display).
showMessageDialog
public static void showMessageDialog(Component parent_,
Object message_)
Brings up a confirmation dialog titled "Confirm"
showMessageDialog
public static void showMessageDialog(Component parent_,
Object message_,
String title_,
int msgtype_)
Brings up a confirmation dialog with the specified title. The
msgtype parameter is ignored (it is used in the javax.swing package to
specify an icon to display).