charvax.swing

Class JOptionPane


public class JOptionPane
extends java.lang.Object

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:

parentComponentparentComponent

options

Field Summary

static int
CANCEL_ACCELERATOR
static String
CANCEL_LABEL
static int
CANCEL_OPTION
static int
CLOSED_OPTION
static int
DEFAULT_OPTION
static int
ERROR_MESSAGE
static int
INFORMATION_MESSAGE
static int
NO_ACCELERATOR
static String
NO_LABEL
static int
NO_OPTION
static int
OK_ACCELERATOR
static int
OK_CANCEL_OPTION
static String
OK_LABEL
static int
OK_OPTION
static int
PLAIN_MESSAGE
static int
QUESTION_MESSAGE
static int
WARNING_MESSAGE
static int
YES_ACCELERATOR
static String
YES_LABEL
static int
YES_NO_CANCEL_OPTION
static int
YES_NO_OPTION
static int
YES_OPTION
protected Object
_initialValue
Option that should be initially selected in _options.
protected String
_inputValue
protected Object
_message
protected int
_messagetype
protected Object[]
_options
Array of options to display to the user in the bottom button-panel.
protected int
_optiontype
Determines which option buttons to display (unless an array of options is explicitly specified with setOptions()).
protected Object
_value
The currently selected option.
protected boolean
_wantsInput
If true, an TextField will be displayed for the user to provide input.

Constructor Summary

JOptionPane()
Creates a JOptionPane with a test message
JOptionPane(Object message_)
Creates an instance of JOptionPane to display the specified message.
JOptionPane(Object message_, int messagetype_)
Creates an instance of JOptionPane to display the specified message.
JOptionPane(Object message_, int messagetype_, int optiontype_)
Creates an instance of JOptionPane to display the specified message.
JOptionPane(Object message_, int messageType_, int optionType_, Object icon_, Object[] options_, Object initialValue_)
Creates an instance of JOptionPane to display the specified message.

Method Summary

JDialog
createDialog(Component owner_, String title_)
Creates and returns a new JDialog for displaying the required message.
Object
getInitialValue()
Returns the initial value that is to be enabled -- the Component that has the focus when the pane is initially displayed.
Object
getInputValue()
Returns the value the user has input, (relevant only if _wantsInput is true).
Object[]
getOptions()
Returns the choices the user can make.
Object
getValue()
Returns the option the user has selected.
boolean
getWantsInput()
Returns the value of _wantsInput.
void
setInitialSelectionValue(Object value_)
Sets the default input value that is displayed to the user.
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.
void
setInputValue(Object value_)
Sets the initial value of the text field for the user to modify.
void
setOptions(Object[] newOptions_)
Sets the options this pane displays in the button-panel at the bottom.
void
setWantsInput(boolean newvalue_)
If newvalue_ is true, a JTextField will be displayed for the user to provide text input.
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.
static String
showInputDialog(Component parent_, Object message_, String title_, int messagetype_)
Brings up a dialog that allows the user to input a value.
static void
showMessageDialog(Component parent_, Object message_)
Brings up a confirmation dialog titled "Confirm"
static void
showMessageDialog(Component parent_, Object message_, String title_, int msgtype_)
Brings up a confirmation dialog with the specified title.

Field Details

CANCEL_ACCELERATOR

public static int CANCEL_ACCELERATOR

CANCEL_LABEL

public static String CANCEL_LABEL

CANCEL_OPTION

public static final int CANCEL_OPTION
Field Value:
302

CLOSED_OPTION

public static final int CLOSED_OPTION
Field Value:
304

DEFAULT_OPTION

public static final int DEFAULT_OPTION
Field Value:
200

ERROR_MESSAGE

public static final int ERROR_MESSAGE
Field Value:
100

INFORMATION_MESSAGE

public static final int INFORMATION_MESSAGE
Field Value:
101

NO_ACCELERATOR

public static int NO_ACCELERATOR

NO_LABEL

public static String NO_LABEL

NO_OPTION

public static final int NO_OPTION
Field Value:
301

OK_ACCELERATOR

public static int OK_ACCELERATOR

OK_CANCEL_OPTION

public static final int OK_CANCEL_OPTION
Field Value:
203

OK_LABEL

public static String OK_LABEL

OK_OPTION

public static final int OK_OPTION
Field Value:
303

PLAIN_MESSAGE

public static final int PLAIN_MESSAGE
Field Value:
104

QUESTION_MESSAGE

public static final int QUESTION_MESSAGE
Field Value:
103

WARNING_MESSAGE

public static final int WARNING_MESSAGE
Field Value:
102

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
Field Value:
202

YES_NO_OPTION

public static final int YES_NO_OPTION
Field Value:
201

YES_OPTION

public static final int YES_OPTION
Field Value:
300

_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.

Constructor Details

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.
Parameters:
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.
Parameters:
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.
Parameters:
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.
Parameters:
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 select
initialValue_ - the choice that is initially selected; if null, then nothing will be initially selected; only meaningful if "options" is used

Method Details

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.
Parameters:
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.
Parameters:
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).