JOptionPane
- Standard object used to popup messages, ask yes/no questions,
and ask for simple input.
- In order to use JOptionPane methods, you must tell java that
you are going to use the javax.swing libaray. do this using the import
statement.
- import statements go at the begining of the program.
import
javax.swing.JOptionPane;
or
import javax.swing.*;
Methods:
- JOptionPane.showMessageDialog
public static void showMessageDialog(Component
parentComponent,
Object message,
String title,
int messageType)
Brings up a dialog that displays a message using an icon determined by
the messageType parameter.
Parameters:
parentComponent - determines the Frame in
which the dialog is displayed; if null, or if the parentComponent has
no Frame, a default Frame is used
message - the Object to display
title - the title string for the dialog
messageType - the type of message to be displayed:
ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE,
or PLAIN_MESSAGE
Example:
JOptionPane.showMessageDialog(null,"Your computer has a virus","Virus checker",
JOptionPane.ERROR_MESSAGE);
public static void showMessageDialog(Component
parentComponent, Object message)
Shortened version, brings up a dialog that displays the message and an
ok button.
Parameters:
parentComponent - determines the Frame in
which the dialog is displayed; if null, or if the parentComponent has
no Frame, a default Frame is used
message - the Object(tex) to display
Example:
JOptionPane.showMessageDialog(null,"Your computer has a virus");
- JOptionPane.showConfirmDialog
public static int
showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType)
Brings up a dialog where the number of choices is determined by the
optionType parameter, where the messageType parameter determines the
icon to display. The messageType parameter is primarily used to supply
a default icon from the Look and Feel.
Parameters:
parentComponent -
determines the Frame in which the dialog is displayed; if null, or if
the parentComponent has no Frame, a default Frame is used.
message - the Object to display in the middle of the
box, the question to ask
title - the title string for the dialog, text displayed in the
blue banner area
optionType - an integer designating the options available on
the dialog: YES_NO_OPTION, or YES_NO_CANCEL_OPTION
messageType - an integer designating the kind of message this
is; primarily used to determine the icon from the pluggable Look and
Feel: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,
QUESTION_MESSAGE, or PLAIN_MESSAGE
Returns:
an integer indicating the option selected by the user, YES_OPTION,
NO_OPTION, CANCEL_OPTION
Example:
int answer = JOptionPane.showConfirmDialog(null, "Do you want to proceed with the installation","Confirmation",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if(answer == JOptionPane.YES_OPTION)
System.out.println("You pressed yes");
public static int
showConfirmDialog(Component parentComponent, Object
message)
Brings up a dialog with the given dialog and the choices Yes, No,
Cancel,
Parameters:
parentComponent -
determines the Frame in which the dialog is displayed; if null, or if
the parentComponent has no Frame, a default Frame is used.
message - the Object to display in the middle of the
box, the question to ask
title - the title string for the dialog, text displayed in the
blue banner area
Returns:
an integer indicating the option selected by the user, YES_OPTION,
NO_OPTION, CANCEL_OPTION
Example:
int answer = JOptionPane.showConfirmDialog(null, "Do you want to proceed with the installation");
if(answer == JOptionPane.YES_OPTION)
System.out.println("You pressed yes");
public static int
showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType)
Brings up a dialog where the number of choices is determined by the
optionType
parameter.
Parameters:
parentComponent -
determines the Frame in which the dialog is displayed; if null, or if
the parentComponent has no Frame, a default Frame is used.
message - the Object to display in the middle of the
box, the question to ask
title - the title string for the dialog, text displayed in the
blue banner area
optionType - an integer designating the options available on
the dialog: YES_NO_OPTION, or YES_NO_CANCEL_OPTION
Returns:
an integer indicating the option selected by the user, YES_OPTION,
NO_OPTION, CANCEL_OPTION
Example:
int answer = JOptionPane.showConfirmDialog(null, "Do
you want to proceed with the installation","Confirmation",
JOptionPane.YES_NO_CANCEL_OPTION);
if(answer == JOptionPane.NO_OPTION)
System.out.println("You pressed no");
-
JOptionPane.showInputDialog
public static String showInputDialog(Component parentComponent,
Object message,
String title,
int messageType)
- Shows a file in the box dialog requesting input from the user
parented to
parentComponent
with the dialog having the
title title
and message type messageType
.
- Parameters:
parentComponent
- the parent Component
for the dialog, or null
message
- the Object
to display
title
- the String
to display in
the dialog title bar
messageType
- the type of message that is to be
displayed: ERROR_MESSAGE
, INFORMATION_MESSAGE
,
WARNING_MESSAGE
, QUESTION_MESSAGE
,
or PLAIN_MESSAGE
Returns:
String entered by the user.
example:
String answer =
JOptionPane.showInputDialog(null,"Please enter your
age","Question",JOptionPane.QUESTION_MESSAGE);
int age = Integer.parseInt(answer);
int dogyears = age*7;
System.out.println("You age in dog years is "+dogyears);