JoptionPane GUI

advertisement
JOptionPane GUI
Customizing title and logo of JOptionPane Dialog box
Output:
Code:
JOptionPane.showMessageDialog(null,
"Danger. Do not press OK.",
"WARNING!! WARNING!!",
JOptionPane.WARNING_MESSAGE);
Explanation
Message: "Danger. Do not press OK.”
Title: "WARNING!! WARNING!!"
Logo/Icon: JOptionPane.WARNING_MESSAGE
JOptionPane Icons
JOptionPane.INFORMATION_MESSAGE
JOptionPane.QUESTION_MESSAGE
JOptionPane.WARNING_MESSAGE
JOptionPane.ERROR_MESSAGE
Adding a Yes, No and Cancel option to JOptionPane Dialog
Output:
Code:
int n = JOptionPane.showConfirmDialog( null, "Do you want
to start the program?", "Welcome",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MES
SAGE);
Explanation
Variable n used to store choice of user. YES, NO and CANCEL have the value 0,1 and 2
respectively similarly to an array. Therefore if n=0 then YES was chosen etc.
Attributes/Syntax of code
Dialog call
JOptionPane.showConfirmDialog
Similar to JOptionPane.showMessageDialog and JOptionPane.showInputDialog
except displays a customised dialog with a variety of buttons.
String message
"Do you want to start the program?"
Message to be displayed.
String title
"Welcome"
The title of the dialog.
optionType
JOptionPane.YES_NO_CANCEL_OPTION
Specifies the set of buttons that appear at the bottom of the dialog. Choose from
one of the following standard sets: DEFAULT_OPTION, YES_NO_OPTION,
YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION.
logoType
JOptionPane.QUESTION_MESSAGE
This argument determines the icon displayed in the dialog. Choose from one of the
following values: PLAIN_MESSAGE (no icon), ERROR_MESSAGE,
INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE.
Customising Button Text
The above dialog displays three fixed options YES, NO and CANCEL. The text of these
buttons can be customised and more buttons can be added to the dialog box.
Output:
Code:
//Creating an array which stores the text of two buttons.
Object[] options = {"Go to school",
"Go home"};
int n = JOptionPane.showOptionDialog(null,
"Where do you want to go?",
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
//do not use a custom Icon
options, //the array created array will be displayed
as buttons with the specified text
options[0]); //the element/button in the array which
will be outlined as the default choice
Explanation(see comments)
If “Go to school” is chosen, then n will be assigned 0 or if “Go home “ is selected then
n=1;
General Format
1. Create array
2. JOptionPane.showOptionDialog(message, title, JOptionPane.YES_NO_OPTION,
logoType, null, array, defaultArrayElement)
Example of Program using JOptionPane Dialog with customised menu buttons,
logo, title and message
import javax.swing.*;
public class gui
{
public static void main (String[]args)
{
String title="Gender";
String message="Click your gender below: ";
Object[] options = {"Male",
"Female","Not sure"};
int n =
JOptionPane.showOptionDialog(null,message,title,JOptionPane.YES_NO_OP
TION, JOptionPane.QUESTION_MESSAGE,null,options,
options[0]);
if (n==0)
{
JOptionPane.showMessageDialog(null,"Gender selected:
Male",title,JOptionPane.WARNING_MESSAGE);
}
else if (n==1)
{
JOptionPane.showMessageDialog(null,"Gender selected:
Female",title,JOptionPane.WARNING_MESSAGE);
}
else if (n==2)
{
JOptionPane.showMessageDialog(null,"You are delirious, please
contact mental institution for
help.",title,JOptionPane.INFORMATION_MESSAGE);
}
}
}
Output:
If n=0 (male selected):
If n=1 (female selected):
If n=2 (not sure selected):
Download