User input with a Java JOptionPane example

Java JOptionPane input example

The best way to teach the concept of Java user input to new software developers is to have them use the extremely visual and highly user-friendly JOptionPane from Swing.

With one simple line of code, Java’s JOptionPane makes it possible to prompt the user with a Windows based, input dialog box, and return any use input to your program as a String.

var name = javax.swing.JOptionPane.showInputDialog("What is your name?");

If the javax.swing package is imported, the JOptionPane code becomes even more succinct. The import also simplifies the use of other fun JOptionPane methods including:

  • showConfirmDialog()
  • showMessageDialog()
  • showOptionDialog()

JOptionPane import and methods

For example, the following Java class uses JOptionPane’s showInputDialog() method to prompt for user input, and then displays a message back to the user with the showMessageDialog() function. As you can see, the javax.swing import greatly reduces the code’s verbosity:

import javax.swing.*;
public class JOptionPaneExample {

  public static void main(String[] args) {
    /* JOptionPane Java user input example */
    var name = JOptionPane.showInputDialog("What is your name?");
    var output = name + " is such a nice name!";
    JOptionPane.showMessageDialog(null, output);

JOptionPane vs Console vs Scanner input in Java

It has always been a mystery to me why instructors who teach Java always subject students to console based input the JDK’s Scanner, Console or InputStream classes.

Using a command prompt or terminal window to input text into a computer program is neither intuitive, nor is it fun. Console input a great way to discourage new learners and push them away from the Java platform.

In contrast to the Console or Scanner classes, the JOptionPane provides immediate visual feedback, which makes writing Java code fun. Furthermore, it teaches newcomers the Java platform some important lessons about the JDK, including:

  • Java user input does not have to be terminal or console based
  • Java supports Windowing components out of the box, unlike languages like Python or Rust
  • Java is cross platform, as the JOptionPane works on Linux, Windows and Macs
simple swing showOptionDialog box

The Java JOptionPane showOptionDialog can take custom input from the user.

JOptionPane showConfirmDialog example

The showInputDialog and showMessageDialog functions are great at garnering user input and displaying a response, but to limit a user’s response to just yes, no or cancel, the JOptionPane’s showConfirmDialog() method can be used.

This function will display a message box with only three options, which will return the following int value to the program:

  • 0 if the user selects ‘Yes’
  • 1 if the user selects ‘No’
  • 2 if the user selects ‘Cancel’

The following example demonstrates the JOptionPane’s showConfirmDialog() method in action:

import javax.swing.*;
public class JOptionPaneExample {

  public static void main(String[] args) {
    /* JOptionPane Java user input example */
    var yesOrNo = JOptionPane.showConfirmDialog(null, "What will it be?");
    if (yesOrNo == 0) {
      JOptionPane.showMessageDialog(null, "You chose yes!");
    if (yesOrNo == 1) { 
      JOptionPane.showMessageDialog(null, "You chose no.");
    if (yesOrNo == 2) { 
      JOptionPane.showMessageDialog(null, "You chose to cancel!");

JOptionPane showOptionDialog method

To limit input, but not restrict the user to a ‘yes’, ‘no’ or ‘cancel’, the JOptionPane showOptionDialog method can be used.

This method allows you to supply an array of objects to the dialog box. Each object is rendered, and the calling program receives the array index position of the option selected.

For example, the following JOptionPane showOptionDialog example asks the user to select either a brownie, pie, cake as a dessert option.

import javax.swing.*;
public class ShowOptionDialogExample {

  public static void main(String[] args) {
    /* JOptionPane Java user input example */    
    String[] options = { "brownie", "pie", "cake" };
    var dessert = JOptionPane.showOptionDialog(null, "Which dessert?", "Select one:", 
                                                      0, 3, null, options, options[0]);
    if (dessert == 0) {
      JOptionPane.showMessageDialog(null, "You chose a brownie!");
    if (dessert == 1) { 
      JOptionPane.showMessageDialog(null, "You chose pie.");
    if (dessert == 2) { 
      JOptionPane.showMessageDialog(null, "You chose cake!");

When this code runs, if the user selects the pie button, then the index of 1 is returned, which generates a JOptionPane message dialog box that displays the text “You chose pie!”

JOptionPane error message dialog boxes

As you can see, the JOptionPane’s show OptionDialog box is highly parameterized. The options available are:

  • The parentComponent, which can be left null unless the dialog box is to be displayed on a custom panel
  • The title of the dialog box
  • The text to display inside the dialog box
  • An option type to include yes, no or cancel buttons
  • The message type, be it warning, informational or a question
  • An optional, custom icon to display
  • The array of values to display
  • The item in the array to initially highlight

How to use Java’s JOptionPane for user input

New users to the Java language should be encouraged to use the JOptionPane for user input. To use a JOptionPane, simply follow these steps:

  1. Import javax.swing.*;
  2. Choose the type of JOptionPane dialog box to use
  3. Parameterize the showXyxDialog method appropraitely
  4. Store any data returned from the JOptionPane in a variable

It’s fun to do windowing programming with Java.

Java’s Swing package has a variety of powerful classes for creating desktop applications that run well on all platforms. And the JOptionPane is a great way to introduce yourself to the world of Swing based programming in Java.