Building an Input Dialog Box

Programmer at a desk with multiple monitors overlooking a city scene

Pexels / Public Domain 

Message dialog boxes are great when you want to inform the user of a message and get a simple response (i.e., a YES or OK click) but there are times when you want the user to give a little bit of data. Maybe your program wants a pop-up window to grab their name or star sign. This can be achieved easily by using the


method of the



The JOptionPane Class

To use the

class you don't need to make an instance of a

because it creates dialog boxes through the use of static methods and ​static fields. It only creates modal dialog boxes which is fine for input dialog boxes because generally, you want the user to input something before your application carries on running.



method is overloaded several times to give you a few options about how the input dialog box appears. It can have a text field, a combo box or a list. Each of these components can have a default value selected.

Input Dialog With a Text Field

The most commonest input dialog simply has a message, a text field for the user to input their response and an OK button:


method takes care of building the dialog window, the text field and OK button. All you have to do is provide the parent component for the dialog and the message to the user. For the parent component I'm using the
keyword to point to the
the dialog is created from. You can use null or specify a name of another container (e.g.,
) as the parent. Defining a parent component enables the dialog to position itself on the screen in relation to its parent. If it is set to null the dialog will appear in the center of the screen.
input variable

captures the text the user enters into the text field.

Input Dialog With a Combo Box

To give the user a selection of choices from a combo box you need to use a String array:

//Options for the combo box dialogString[]
choices = {"Monday", "Tuesday"
,"Wednesday", "Thursday", "Friday"};
//Input dialog with a combo box
String picked = (String)JOptionPane.showInputDialog(this, "Pick a Day:"
, "ComboBox Dialog", JOptionPane.QUESTION_MESSAGE
 , null, choices, choices[0]);

As I am passing a String array for the selection values the method decides a combo box is the best way to present those values to the user. This


method returns an


and because I want to get the text value of the combo box selection I've defined the return value to be a (



Also note that you can use one of OptionPane's message types to give the dialog box a certain feel. This can be overridden if you pass an icon of your own choosing.

Input Dialog With a List

If the


A full Java code example can be viewed in Input Dialog Box Program. If you're interested in seeing the other dialog boxes the JOptionPane class can create then have a look at the JOptionPane Option Chooser Program.

mla apa chicago
Your Citation
Leahy, Paul. "Building an Input Dialog Box." ThoughtCo, Feb. 16, 2021, Leahy, Paul. (2021, February 16). Building an Input Dialog Box. Retrieved from Leahy, Paul. "Building an Input Dialog Box." ThoughtCo. (accessed June 6, 2023).