JOptionPane Class

Package: javax.swing

JOptionPane has a number of static methods that display generic dialog boxes for simple user interaction. It is commonly used to display error or informational messages, to get the user’s confirmation to proceed with an operation, or to get a single input value.

Fields

Button Option Field

Description

static int YES_NO_OPTION

Yes or No.

static int YES_NO_CANCEL_OPTION

Yes, No, or Cancel.

static int OK_CANCEL_OPTION

OK or Cancel.

Message Type Field

Description

static int ERROR_MESSAGE

Error message.

static int INFORMATION_MESSAGE

Informational message.

static int WARNING_MESSAGE

Warning message.

static int QUESTION_MESSAGE

Question message.

static int PLAIN_MESSAGE

A plain message with no icon.

Return Value Field

Description

static int YES_OPTION

The user clicked Yes.

static int NO_OPTION

The user clicked No.

static int OK_OPTION

The user clicked OK.

static int CANCEL_OPTION

The user clicked Cancel.

static int CLOSE_OPTION

The user closed the dialog box.

Methods

Method

Description

static int showConfirm Dialog(Component parent, Object message)

Displays the indicated message and offers the user three choices: Yes, No, and Cancel. The user’s choice is indicated by the return value. The dialog box’s title defaults to Select an Option.

static int showConfirm Dialog(Component parent, Object message, String title)

Displays the indicated message and offers the user three choices: Yes, No, and Cancel. The user’s choice is indicated by the return value. The dialog box’s title is set by the title parameter.

static int showConfirm Dialog(Component parent, Object message, String title, int option)

Displays the indicated message and offers the user three choices: Yes, No, and Cancel. The user’s choice is indicated by the return value. The dialog box’s title is set by the title parameter. The option parameter determines which buttons are displayed; the choices are the Option Type fields listed earlier.

static int showConfirm Dialog(Component parent, Object message, String title, int option, int type)

Displays the indicated message and offers the user three choices: Yes, No, and Cancel. The user’s choice is indicated by the return value. The dialog box’s title is set by the title parameter. The option parameter determines which buttons are displayed; the choices are the option type fields listed earlier. The type parameter determines the message type; the choices are the message type fields listed earlier.

static String showInputDialog (Component parent, Object message)

Requests an input string from the user, displaying the indicated message as a prompt. The value entered by the user is returned.

static String showInput Dialog(Component parent, Object message, String title)

Requests an input string from the user, displaying the indicated message as a prompt. The value entered by the user is returned. The dialog box’s title is set by the title parameter.

static void show MessageDialog (Component parent, Object message)

Displays a message in a dialog box.

static void show MessageDialog (Component parent, Object message, String title, int type)

Displays a message in a dialog box with the specified title. The message type is indicated by the type parameter.

Note that the parent parameter in each method listed can be either a Swing component or null. If you specify a component, the dialog box is centered over the component. If you specify null, the dialog box is centered on the user’s screen.

To display a simple informational message, use JOptionPane like this:

JOptionPane.showMessageDialog(null,”Hello!”);

You can specify a title for the message dialog box and use any of the message type fields to force a specific icon like this:

JOptionPane.showMessageDialog(null,”Hello!”,

“This is a Warning!”, JOptionPane.WARNING_MESSAGE);

To display a confirmation dialog box, use one of the showConfirmDialog methods. For example:

int result = JOptionPane.showConfirmDialog(null,

“Are you sure?”,

“Please Confirm”,

JOptionPane.YES_NO_OPTION);

Having saved the result of the dialog box in result, you can then test it like this:

if (result == JOptionPane.YES_OPTION)

// Code if user clicked Yes

else

// Code if user clicked other than Yes

To get an input value from the user, use the showInputDialog method. For example:

String s = JOptionPane.showInputDialog(null,

“Please enter your name:”,

“Need Input!”,

JOptionPane.OK_CANCEL_OPTION);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset