All About JOptionPane Control in Java

All About JOptionPane Control in Java

What is JOptionPane ?
JOptionPane Contains classes used for a graphical user interface (GUI) Facilitates data entry and data output. class JOptionPane contains methods that display a dialog box. The JOptionPane class provides static methods to display each type of dialog box.

Swing JOptionPane allows you to
      » Allows you to create a dialog box
      » Display information
      » Request information
      » Give the user a choice with buttons
The JOptionPane class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static ShowxxxDialog methods shown below:

Method NameDescription
showConfirmDialogAsks a confirming question, like yes/no/cancel.
showInputDialogPrompt for some input.
showMessageDialogTell the user about something that has happened.
showOptionDialogThe Grand Unification of the above three.


Parameters:
The parameters to these methods follow consistent patterns:
parentComponent
Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be null, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).
message
A descriptive message to be placed in the dialog box. In the most common usage, message is just a String or String constant. However, the type of this parameter is actually Object. Its interpretation depends on its type:
Object[]
An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive -- each object in the array is interpreted according to its type.
Component
The Component is displayed in the dialog.
Icon
The Icon is wrapped in a JLabel and displayed in the dialog.
others
The object is converted to a String by calling its toString method. The result is wrapped in a JLabel and displayed.
messageType
Defines the style of the message. The Look and Feel manager may lay out the dialog differently depending on this value, and will often provide a default icon. The possible values are:
    » ERROR_MESSAGE
    » INFORMATION_MESSAGE
    » WARNING_MESSAGE
    » QUESTION_MESSAGE
    » PLAIN_MESSAGE
optionType
Defines the set of option buttons that appear at the bottom of the dialog box:
    » DEFAULT_OPTION
    » YES_NO_OPTION
    » YES_NO_CANCEL_OPTION
    » OK_CANCEL_OPTION
You aren't limited to this set of option buttons. You can provide any buttons you want using the options parameter.
options
A more detailed description of the set of option buttons that will appear at the bottom of the dialog box. The usual value for the options parameter is an array of Strings. But the parameter type is an array of Objects. A button is created for each object depending on its type:
Component
The component is added to the button row directly.
Icon
A JButton is created with this as its label.
other
The Objects is converted to a string using its toString method and the result is used to label a JButton.
icon
A decorative icon to be placed in the dialog box. A default value for this is determined by the messageType parameter.
title
The title for the dialog box.

How To Use?
If you didn't get the above theory related to JOptionPane and just want to know how to use it! Then here's a source code of a Java GUI program in which all the functions of JOptionPane are used. You can edit the code or extract what you want from the code and still you have any problem than at the end of this post there's a download link of this project (NetBeans Project Folder) and also an .exe file of this program you can download that and understand its way of working.

Source Code:
//Global variables
int messageType = 0 , buttonType=0;
String optString = "Button1,Button2,Button3";
String[] opt=optString.split(",");
//Enable/Desable radio buttons
void disableButtons(){
    b1.setEnabled(false);
    b2.setEnabled(false);
    b3.setEnabled(false);
    b4.setEnabled(false);
}
void enableButtons(){
    b1.setEnabled(true);
    b2.setEnabled(true);
    b3.setEnabled(true);
    b4.setEnabled(true);
}
String choose(int n){
    if(n==0){
        return "clicked on first button";
    } else if(n==1){
        return "clicked on second button";
    } else if(n==2){
        return "clicked on cancel button";
    } else{
        return "closed the dialog box directly";
    }
}
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
//Get Choosen icon
if(c1.isSelected()){
    messageType=-1;
} else if(c2.isSelected()){
    messageType=1;
} else if(c3.isSelected()){
    messageType=3;
} else if(c4.isSelected()){
    messageType=2;
} else if(c5.isSelected()){
    messageType=0;
}
//Getting chossen button type
if(b1.isSelected()){
    buttonType=-1;
} else if(b2.isSelected()){
    buttonType=0;
} else if(b3.isSelected()){
    buttonType=1;
} else if(b4.isSelected()){
    buttonType=2;
}
//Showing dialog box according to user selection
if(d1.isSelected()){
//Using Message Dialog
    JOptionPane.showMessageDialog(this, "This is Message Dialog", "Title", messageType);
} else if(d2.isSelected()){
// Using Conformation Dialog
    int n = JOptionPane.showConfirmDialog(this, "This is Confirm Dialog", "Title", buttonType, messageType);
    logs.setText(choose(n));
} else if(d3.isSelected()){
//Using Input Dialog
    String n = JOptionPane.showInputDialog(this, "This is Input Dialog", "Title", messageType);
    logs.setText("You Entered:"+n);
} else if(d4.isSelected()){
//Using Option Dialog
    int n =JOptionPane.showOptionDialog(this, "This is Option Dialog", "Title", buttonType, messageType, null, opt, null);
    logs.setText("You Choosed:"+opt[n]);
}
    }    
Output:
JOptionPane in java output image

Download:
If the above code confused you, than download the whole source code (created in NetBeans) and also the build program in .exe format.

Source Code : Download
Biuld   (.exe): Download


Source: Oracle , javabeginner

Leave a Reply

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.