Java's JOptionPane showOptionDialog by Example

How to use the JOptionPane’s showOptionDialog method

Java’s JOptionPane provides a simple way to read input from the user and display information back.

There are many helpful functions included in the JOptionPane’s list of methods, including:

  • showInputDialog
  • showMessageDialog
  • showConfirmDialog
  • showOptionDialog

Each of these functions is relatively straight forward with the exception of the JOptionPane’s showOptionDialog method.

JOptionPane showOptionDialog examples

To help demystify the most complicated of these, let’s take a look at a few different JOptionPane showOptionDialog examples.

The JOptionPane’s showOptionDialog method requires an array of text Strings.

Each element in the array is displayed as a clickable button. When a button is clicked, the windows closes and the array index of the element selected is returned to the program.

Here’s a simple showOptionDialog  example. An image of the dialog box this code generates can be seen below.

simple swing showOptionDialog box

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

import javax.swing.*;
public class ShowOptionDialogExample {

  public static void main(String[] args) {
    /* Simple JOptionPane ShowOptionDialogJava example */    
    String[] options = { "rock", "paper", "scissors" };
    var selection = JOptionPane.showOptionDialog(null, "Select one:", "Let's play a game!", 
                                                      0, 3, null, options, options[0]);
    if (selection == 0) {
      JOptionPane.showMessageDialog(null, "You chose rock!");
    }
    if (selection == 1) { 
      JOptionPane.showMessageDialog(null, "You chose paper.");
    }
    if (selection == 2) { 
      JOptionPane.showMessageDialog(null, "You chose scissors!");
    }
  }
}

Java’s showOptionDialog method explained

These are the two most important lines of code from the showOptionDialog example above:

String[] options = { "rock", "paper", "scissors" };
var selection = JOptionPane.showOptionDialog(null, "Select one:", "Let's play a game!", 
                                                      0, 3, null, options, options[0]);

The array named options defines the three fields to display in the dialog box.

The array is then passed as the seventh of eight parameters required by the JOptionPane’s showOptionDialog method.

When the dialog box appears, the names of the elements in the array appear as clickable buttons.

The array index of the button clicked is returned to the program.

The showOptionDialog’s many arguments

The fact that the showOptionDialog function takes eight parameters can make it both intimidating to use and difficult to understand.

Here are the eight fields the showOptionDialog method defines, along with how they can be used in your application:

  1. The first parameter is the parentComponent, which can be set to null if the dialog box is to be rendered independently
  2. The second parameter is the prompt to be displayed inside the dialog box
  3. The third parameter is the text to be displayed in the title of the dialog box
  4. The fourth parameter is the optionType, which will be the number 0,1,2, or 3, to specify whether yes, no or cancel buttons will appear
  5. The fifth parameter is the messageType, which will be the number 0,1 or 2 or 3 to specify whether an error, info, warning, question or default message icon will appear
  6. The sixth parameter is an optional image icon to display in the message box
  7. The seventh parameter is the array of options to use
  8. The either parameter is the array object to select as the default value

Simple showOptionDialog defaults

To simplify the use of the JOptionPane’s showOptionDialog method, every parameter except the value of the array can be set to null or zero and the dialog box will function properly:

String[] options = { "brownie", "pie", "cake" };
int response = JOptionPane.showOptionDialog(null, null, null, 0, 0, null, options, null);

Furthermore, the JOptionPane class defines enums for the optionType and messageType parameters, as demonstrated below:

String[] options = { "brownie", "pie", "cake" };
int response = JOptionPane.showOptionDialog(null, null, null, 
                                            JOptionPane.DEFAULT_OPTION, 
                                            JOptionPane.QUESTION_MESSAGE, 
                                            null, options, null);

Advanced JOptionsPane’s showOptionDialog example

To make the most use of the JOptionsPane’s showOptionDialog method, a developer should take advantage of all of the available parameters.

The following advanced JOptionsPane’s showOptionDialog example takes advantage of each parameter, adding a red JFrame upon which to display the dialog box, and an image icon featuring the Java mascot duke. An image of the JOptionPane this code creates can be seen below.

import java.awt.Color;
import javax.swing.*;

public class JOptionPaneExample {

    public static void main(String[] args) {
        String[] options = { "brownie", "pie", "cake" };
        int x = JOptionPane.showOptionDialog(null, null, null, 
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, options, null);
        
        JFrame frame = new JFrame();
        frame.getContentPane().setBackground(Color.blue);
        frame.setBounds(300, 300, 500, 350);
        frame.setVisible(true);

        int dessert = JOptionPane.showOptionDialog(frame, 
                "Which dessert would you like?", "Select a dessert", 
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                new ImageIcon("C:\\_tools\\java-duke-sm.jpg"), 
                options, options[0]);
        
        if (dessert == 0) {
            JOptionPane.showMessageDialog(frame, "You chose a brownie.");
        }
        if (dessert == 1) {
            JOptionPane.showMessageDialog(frame, "You chose pie.");
        }
        if (dessert == 2) {
            JOptionPane.showMessageDialog(frame, "You chose cake.");
        }
    }
}

When the JOptionPane showMessageDialog() function runs, the dialog box appears on top of a blue frame, with Java’s Duke mascot as the icon.

Swing JOptionPane showOptionDialog

When fully parameterized, the JOptionPane showOptionDialog displays on a frame with a custom icon.

Garnering user input in Java programs can be difficult, but the JOptionPane’s showMessageDialog() makes it possible to create engaging, visual input forms that will improve the experience of your users.

 

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close