JOptionPane: showInputDialog

The JOptionPane class provides several types of dialog boxes, and among them is the input dialog that allows data entry.

The showInputDialog method of the JOptionPane class allows you to enter a string or choose from a JComboBox list. The return call of this function is a string. of characters entered or an instance of  Object if you are using a JComboBox, otherwise null in  the data entry is invalid.

The display of the dialog message by the java.swing.JOptionPane class is provided by the method:

String response = showInputDialog(component, message, title, type);
  • Component: is the calling Component window or object;
  • message: the message to be displayed;
  • title: the title of the dialog box;
  • type: is the message type of JOptionPane:
    • ERROR_MESSAGE
    • PLAIN_MESSAGE
    • WARNING_MESSAGE
    • QUESTION_MESSAGE
    • INFORMATION_MESSAGE
  • response: the returned value.
Here is a simple example to test the showInputDialog():

import javax.swing.JFrame; 
import javax.swing.JOptionPane;

public class Inputdialog {

public static void main(String[] args) {
JFrame frame = new JFrame("");
String return = JOptionPane.showInputDialog(
frame, "Enter your name","Dialog",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
Output
JOptionPane showInputDialog

JOptionPane with a JComboBox

Another method allows us to create a Dialog that contains a combo choice box JComboBox with  showInputDialog:

Object object = showInputDialog(Component parent,
                            Object message,
                      Title String,
                      int typeMessage,
                      Icon icon,                    // insert ImageIcon
                      Object[] selection,      // Selecting values in the form of an array of type Object
                      InitialValue Object) // the initial object that will be displayed

import javax.swing.JFrame; 
import javax.swing.JOptionPane;

public class Inputdialog {

public static void main(String[] args) {
JFrame frame = new JFrame("");

Object[] selection = {1,2,3,4,5,6};
String return = (String) JOptionPane.showInputDialog(
frame, "Choose an index",
"Dialog",
JOptionPane.QUESTION_MESSAGE,
null, selection, selection[0]);
System.exit(0);
}
}
Execution:
JOptionPane showInputDialog JComboBox

In this example, the message type is a  QUESTION_MESSAGE so, the logo as an exclamation mark is displayed by default.
We did a cast (String) because the method returns an Object.

Add an icon in showInputDialog

In the previous example, we set null instead of creating an ImageIcon that is displayed instead of the default icon. This code replaces null with the variable icon:

ImageIcon icon = new ImageIcon("question.png"); 
Object[] selection = {1,2,3,4,5,6};
String back = (String) JOptionPane.showInputDialog(frame, "Choose an index",
"Dialog",JOptionPane.QUESTION_MESSAGE,
icon, selection, selection[0]);
Execution:
insert showInputDialog icon

References:
upmf-grenoble: Swing: JOptionPane
Java doc: showInputDialog method