JOptionPane class in Java is a good option to create windows like interaction in Java Applications. It includes a standard dialog box that asks the users to input a value or display a message. The dialog boxes in class JOptionPane are flexible enough to create different kinds of messages and user prompts.
Methods of JOptionPane Class in Java
JOptionPane has four methods under the category of showXDialog where X stands for Confirm, Input, Message and Option.
showConfirmDialog
This dialogbox can be used when you want the user to answer a question in Yes, No, or Cancel. For example before deleting a record from database table you want user to confirm.
JOptionPane.showConfirmDialog(null, "DO You Want to Exit", "Choose Your Option", JOptionPane.YES_NO_OPTION);
The above code displays this dialogbox.
This dialog box returns the option selected as an integer value. It can be stored in a variable and compared with the defined constants
- YES_OPTION
- NO_OPTION
- CANCEL_OPTION
- OK_OPTION
- CLOSED_OPTION
Example
int yn; yn=JOptionPane.showConfirmDialog(null, "DO You Want to Exit", "Choose Your Option", JOptionPane.YES_NO_OPTION); if (yn==JOptionPane.NO_OPTION) { } else { }
showInputDialog
This dialogbox defined in JOptionPane class in Java is used when you wish to get some input from the user. Like if you want user to enter his bank account number to display his transaction details you can use showInputDialog to prompt user. After entering the value in the textbox when user presses OK button the input gets stored in a string variable on left hand variable of the assignment operator.
String nam; nam=JOptionPane.showInputDialog("Enter your name");
showMessageDialog
This is an informational dialogbox that doesn’t ask a question or expects user input. It just informs the user about some activity or gives a message. It has only OK button that must be clicked to close the dialog box.
JOptionPane.showMessageDialog(null, "Your Name is" +nam, "Your input is", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null, "caution", "entered a wrong value", JOptionPane.ERROR_MESSAGE);
showOptionDialog
The last dialogbox is the amalgamation of all the previous three dialogboxes. The last attribute of this dialog box is used to define the default selected option. In below example options[0] indicates that the OK button will be selected by default. Similarly in the next dialogbox food[2] option specifies that ‘Pasta’ option is selected by default. You can use any valid index value from the object array to choose the default option.
Object[] options = { "OK", "CANCEL" }; JOptionPane.showOptionDialog(null, "select OK to move further", "Caution", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
Object[] food = { "Pizza", "Fries","Pasta" }; Object youlike = JOptionPane.showInputDialog(null, "What are you Hungry for?", "Choose your Food", JOptionPane.INFORMATION_MESSAGE, null, food, food[2]);
In this example we have created an object array of different food items. They are presented to the user in the form of a combobox from which user can choose one value and click OK or cancel this dialogbox by clicking Cancel button.
Example of JOptionPane Class in Java dialog boxes
Create an application that prompt user to enter two numbers. The program displays the sum, product, difference and division of these two numbers.
import javax.swing.JOptionPane; public class calculator { public static void main(String args[]) { int num1; int num2; int sum; int sub; int mul; int div; String Res; num1=Integer.parseInt(JOptionPane.showInputDialog("Number1=")); num2=Integer.parseInt(JOptionPane.showInputDialog("Number2=")); sum=num1+num2; sub=num1-num2; mul=num1*num2; div=num1/num2; Res="The Sum of "+num1+" and "+num2+" is " +sum ; Res=Res +"\n The DIfference of "+num1+" and "+num2+" is " +sub; Res=Res +"\n The Product of "+num1+" and "+num2+" is " +mul; Res=Res +"\n The Division of "+num1+" by "+num2+" is " +div; JOptionPane.showMessageDialog(null,Res,"Result", JOptionPane.PLAIN_MESSAGE); } }