1. Technology

Building an Input Dialog Box

By

Man using laptop at his desk
Cadalpe/Image Source RF/Getty Images

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 showInputDialog method of the JOptionPane class.

The JOptionPane Class

To use the JOptionPane class you don't need to make an instance of a JOptionPane 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.

The showInputDialog 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:

//Input dialog with a text field
String input =  JOptionPane.showInputDialog(this 
       ,"Enter in some text:");

The showInputDialog 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 this keyword to point to the JFrame the dialog is created from. You can use null or specify a name of another container (e.g., JFrame, JPanel) 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.

The 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 dialog
String[] 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 showInputDialog method returns an Object and because I want to get the text value of the combo box selection I've defined the return value to be a (String).

Also note that you can use one of JOptionPane's message types to give the dialog box a certain feel (see Creating a Message Box - Part I). This can be overridden if you pass an icon of your own choosing.

Input Dialog With a List

If the String array you pass to the showInputDialog method has 20 or more entries then instead of using a combo box it will decide to show the selection values in a list box.

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.

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Using Dialog Boxes
  6. Java Syntax: Building an Input Dialog Box

©2014 About.com. All rights reserved.