JoptionPane In Java

JoptionPane Class:-

009654188_1-7d0b5e4f527ab87e8b4249fd5af6094b

Data Input

● There are 2 ways to take input in Java 

JOptionPane class 

Scanner class

 

● JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something 




1)showConfirmDialog

Image result for showConfirmDialog

 

● This method is a quick and easy way to get user input by asking a confirming question, like yes/no/cancel . 
● The showConfirmDialog() can be called using the following combinations of parameters:
Component, Object 
Component, Object, String, int 
Component, Object, String, int, int 
Component, Object, String, int, int, Icon

Component – The first parameter is a Component which determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used. 
Object – The second parameter can be any Object. (In some older versions of Java you might get a compiler error when using primitive types directly) 
String – The third parameter is a String placed as the title of the confirmDialog window. 
int – The int that follows the String is the OptionType. The different OptionTypes for JOptionPane, are: 

–   DEFAULT_OPTION 
–   YES_NO_OPTION 
–   YES_NO_CANCEL_OPTION 
–    OK_CANCEL_OPTION



int – The next int is the MessageType. The different MessageTypes for JOptionPane, are: 
–    ERROR_MESSAGE 
–    INFORMATION_MESSAGE 
–    WARNING_MESSAGE 
–    QUESTION_MESSAGE 
–    PLAIN_MESSAGE 

● Icon – The last parameter is an Icon that is displayed inside the dialog and overrides the default MessageType icon.




1. Component & Object 


● Simplest way to get user input. 
● The showConfirmDialog() will bring up a dialog with the options Yes, No and Cancel and the title “Select an Option”:

Syntex:-

 int input = JOptionPane.showConfirmDialog(null, "Do you like Java?");
        // 0=yes, 1=no, 2=cancel       
         System.out.println(input);

2. Component, Object, String & int

● Adding some more information to the confirmation dialog. 
● In this example we get to choose the title of the dialog as well as the optionType. 
● The DEFAULT_OPTION has only an “OK” button. 
● This form of the confirmation dialog is equivalent to a simple showMessageDialog() while giving us the ability to get the user input.

3. Component, Object, String, int & int 


● Give your confirmation dialog with error icon: 

Example

 int input = JOptionPane.showConfirmDialog(null, "Do you want to proceed?", "Select an Option...", 
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);

4. Component, Object, String, int, int & icon 


● Make your confirmation dialog “prettier” 
● Example with Icon retrieved from a directory:

2) showInputDialog

 

Related image


● With this method we can prompt the user for input while customizing our dialog window. 

● The showConfirmDialog returns either String or Object and can be called using the following combinations of parameters: 

1. Object (returns String) – Shows a question-message dialog requesting input from the user. 
2. Object, Object (returns String) – Shows a question-message dialog requesting input from the user with the input value initialized.
3. Component, Object (returns String) – Shows a question-message dialog requesting input from the user. Returns the input as String. The Component determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used. 
4. Component, Object, Object (returns String) – Same as above. The only difference is that the input field has an initial value set through the last Object parameter. 
5. Component, Object, String, int (returns String) – Shows a dialog requesting input from the user. The dialog has a title set through the String parameter and a MessageType set through the int parameter. The different MessageTypes for JOptionPane, are: 

ERROR_MESSAGE, 
INFORMATION_MESSAGE,
WARNING_MESSAGE,
QUESTION_MESSAGE, 
PLAIN_MESSAGE

3) showMessageDialog

Image result for showMessageDialog

● It is used to create a message dialog with given title and messageType. 

Syntex

void showMessageDialog(Component parentComponent, Object message, String title, int messageType)

4) showOptionDialog

Image result for showOptionDialog

 

● The showOptionDialog method of JOptionPane is the Grand Unification of showConfirmDialog, showInputDialog and showMessageDialog. 

● The showOptionDialog returns an integer which represents the position of the user’s choice in the Object[].

JOptionPane.showOptionDialog(null        // position the dialog, the default position is center.

, “Message?”                                              // display Message , 
“Title”                                                       // Title in titlebar ,
JOptionPane.NO_OPTION                     // Option type ,
JOptionPane.PLAIN_MESSAGE          // messageType , 
null                                                         // Icon (none) , 
buttons                                                    // Button text as above. 
,“default”);                                              // Default button’s label

Using JoptionPane Class for GUI I/O

import javax.swing.JOptionPane;
 JOptionPane.showInputDialog("Enter Value 1:") 
● JOptionPane.showMessageDialog (null, "Messagehere", "Title here", JoptionPane.PLAIN_MESSAGE); 
● JOptionPane.PLAIN_MESSAGE // this is a plain message 
● JOptionPane.INFORMATION_MESSAGE // this is a info message 
● JOptionPane.ERROR_MESSAGE // this is a error message 
● JOptionPane.WARNING_MESSAGE // this is a warning message 





                             ALso You Can Check Out OUr JAva Artical JUst Click --> HearImage result for click hear


NeXt Artical Related Scanner Class


Image result for Scanner Class

 

Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)


Post a Comment

0 Comments