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 |
|
Yes or No. |
|
Yes, No, or Cancel. |
|
OK or Cancel. |
Message Type Field |
Description |
|
Error message. |
|
Informational message. |
|
Warning message. |
|
Question message. |
|
A plain message with no icon. |
Return Value Field |
Description |
|
The user clicked Yes. |
|
The user clicked No. |
|
The user clicked OK. |
|
The user clicked Cancel. |
|
The user closed the dialog box. |
Methods
Method |
Description |
|
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 |
|
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 |
|
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 |
|
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 |
|
Requests an input string from the user, displaying the indicated message as a prompt. The value entered by the user is returned. |
|
|
|
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 |
|
Displays a message in a dialog box. |
|
Displays a message in a dialog box with the specified title. The message type is indicated by the |
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);