I thought I'd share a collection of JOptionPane showInputDialog examples today. I'll start with the easiest example first, then try to add a little complexity as we go along.
First up, here's a simple JOptionPane showInputDialog example where I display a dialog that prompts a user to enter their name:
import javax.swing.*; /** * JOptionPane showInputDialog example #1. * A simple showInputDialog example. * @author alvin alexander, http://alvinalexander.com */ public class JOptionPaneShowInputDialogExample1 { public static void main(String[] args) { // a jframe here isn't strictly necessary, but it makes the example a little more real JFrame frame = new JFrame("InputDialog Example #1"); // prompt the user to enter their name String name = JOptionPane.showInputDialog(frame, "What's your name?"); // get the user's input. note that if they press Cancel, 'name' will be null System.out.printf("The user's name is '%s'.\n", name); System.exit(0); } }
Here's what this dialog looks like when this code is compiled and run:
As noted in the source code above, if the user selects the Cancel button, or presses the [Esc] key to dispose this dialog, the String
that is returned by the showInputDialog
method will be null
. In either event, in this example, we get whatever String the dialog returns, and we print it.
From a UI perspective, note that the title of the input dialog this code displays simply shows "Input". For a real world application I don't like that very much, so let's take a look at an example where we set the title ourselves.
JOptionPane showInputDialog example #2
In this second JOptionPane example, I'll use a different version of the showInputDialog method that lets you specify the title for our dialog. While we're in the neighborhood, I'll also control the icon that is shown on the dialog, in this case a warning icon:
import javax.swing.*; /** * JOptionPane showInputDialog example #2. * @author alvin alexander, http://alvinalexander.com */ public class JOptionPaneShowInputDialogExample2 { public static void main(String[] args) { JFrame frame = new JFrame("InputDialog Example #2"); String code = JOptionPane.showInputDialog( frame, "Enter the secret code to continue (label)", "Secret code needed (title)", JOptionPane.WARNING_MESSAGE ); // if the user presses Cancel, this will be null System.out.printf("The secret code is '%s'.\n", code); System.exit(0); } }
JOptionPane showInputDialog example #3: Display a combo box (drop down)
In this next example I'll display a combo box (a drop-down list) to the end user, giving them a simple list of options to choose from. I'm always thinking about pizza, so we'll display a combo box with a list of four different pizza types to choose from.
Here's the source code for out third JOptionPane showInputDialog example:
import javax.swing.*; /** * JOptionPane showInputDialog example #3. * Using a combo box in an input dialog (showInputDialog). * * @author alvin alexander, http://alvinalexander.com */ public class ComboxBoxShowInputDialogExample { public static final String[] pizzas = { "Cheese", "Pepperoni", "Sausage", "Veggie" }; public static void main(String[] args) { JFrame frame = new JFrame("Input Dialog Example 3"); String favoritePizza = (String) JOptionPane.showInputDialog(frame, "What is your favorite pizza?", "Favorite Pizza", JOptionPane.QUESTION_MESSAGE, null, pizzas, pizzas[0]); // favoritePizza will be null if the user clicks Cancel System.out.printf("Favorite pizza is %s.\n", favoritePizza); System.exit(0); } }
Here's what this third JOptionPane example looks like when it's running:
As you can see, this JOptionPane showInputDialog example creates a simple combobox that lets the user select one item from the list.
JOptionPane message types
As you've seen in these few examples, there are several message types you can select from when display an input dialog. Here's a quick list of the message types that are available to you:
- ERROR_MESSAGE
- INFORMATION_MESSAGE
- PLAIN_MESSAGE
- QUESTION_MESSAGE
- WARNING_MESSAGE
As we saw in a code example above, you access these error message types as static fields of the JOptionPane
class, like this:
JOptionPane.QUESTION_MESSAGE