Science, Tech, Math › Computer Science Building an Input Dialog Box Share Flipboard Email Print Pexels / Public Domain Computer Science Java Programming PHP Programming Perl Python Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Paul Leahy Computer Science Expert M.A., Advanced Information Systems, University of Glasgow Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based developer. our editorial process Paul Leahy Updated July 03, 2019 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 class. The JOptionPane Class To use the JOptionPaneclass 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. The 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: The showInputDialogmethod 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., 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 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 bo choices = {"Monday", ,"Wednesday", "Thursday", & //Input dialog String picked = (String)JOptionPane.showInputDialog(this, " , "ComboBox Dialog", JOptionPane , null, choi 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 String showInputDialog method has 20 or more entries then instead of using a combo box it will decide to show the selectio 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.