HOWTO · Java
How to Create a Dropdown Menu in Java
This tutorial teaches how to create a dropdown menu in Java.
On this page
In Swing, use JOptionPane for a one-time choice in a dialog, or JComboBox when the dropdown is part of a window. The dialog method returns the selected value when the user confirms; the embedded control keeps its current selection available to the rest of the window.
Create a Dropdown Menu Using JOptionPane in Java
JOptionPane.showInputDialog can show an array of choices and return the selected item. Its arguments include the parent component, prompt, dialog title, message type, optional icon, choices, and initial selection. The initial selection must be one of the choices. In this example, options[1] makes Orange the default. When the user confirms, the method returns the chosen string. If they dismiss the dialog, it returns null, so handle that result before using the selection.
Run this example in the console to see the default selection. Set the Java system property show.gui to true to open the Swing dialog on a graphical desktop:
import javax.swing.JOptionPane;
public class DropDown {
public static void main(String[] args) {
String[] options = {"Apple", "Orange", "Banana", "Pineapple", "None of the listed"};
String selectedFruit;
if (Boolean.getBoolean("show.gui")) {
selectedFruit = (String) JOptionPane.showInputDialog(
null,
"What fruit do you like the most?",
"Choose Fruit",
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]);
} else {
selectedFruit = options[1];
}
if (selectedFruit == null) {
System.out.println("No fruit selected.");
} else {
System.out.println("Your chosen fruit: " + selectedFruit);
}
}
}
Your chosen fruit: Orange
With the default console run, the program prints the initial choice. With show.gui enabled, the user can change it; confirming prints that value, while dismissing the dialog prints the cancellation message. A graphical desktop is required for the dialog path.
Create a Dropdown Menu Using JComboBox in Java
Use JComboBox when the user should make a selection within a window. The example below has a console run that prints a representative choice, plus an optional GUI path that displays the actual dropdown, a Done button, and a result label. A JPanel with FlowLayout lays out the components and lets the label use its preferred size, so it does not overlap the button. The listener reads getSelectedItem() when the button is clicked and updates the label.
Run the example in the console to see the sample selection. Set show.gui to true to display the Swing window:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ComboBoxExample {
public static void main(String[] args) {
if (Boolean.getBoolean("show.gui")) {
SwingUtilities.invokeLater(() -> {
String[] options = {"Apple", "Orange", "Banana", "Pineapple", "None of the listed"};
JComboBox<String> fruitBox = new JComboBox<>(options);
JButton doneButton = new JButton("Done");
JLabel resultLabel = new JLabel("Choose a fruit, then click Done.");
doneButton.addActionListener(event ->
resultLabel.setText("You selected " + fruitBox.getSelectedItem()));
JPanel panel = new JPanel(new FlowLayout());
panel.add(fruitBox);
panel.add(doneButton);
panel.add(resultLabel);
JFrame frame = new JFrame("Choose a Fruit");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
} else {
String selectedFruit = "Orange";
System.out.println("You selected " + selectedFruit);
}
}
}
You selected Orange
In the graphical path, JComboBox initially selects its first item, Apple. The action listener reads the current selection only after Done is clicked; the label then shows that value. The console path prints a fixed sample so the selection flow can be demonstrated without opening a window.
Choose JOptionPane for a brief, one-time prompt whose result is handled immediately. Choose JComboBox when the selection is one control in a larger JFrame, or when the user may change it while the window remains open. Both GUI paths require a graphical desktop; the default console paths run without creating Swing windows.