Create a Dropdown Menu in Java

  1. Create a Drop Down Menu Using JOptionPane in Java
  2. Create a Dropdown Menu Using JComboBox in Java

In this tutorial, we will learn how we can create a dropdown menu in Java using two methods. Both the techniques are used to create GUI components, and the dropdown menu is one of them.

Create a Drop Down Menu Using JOptionPane in Java

The JOptionPane class is a part of the javax.swing package, used mostly to create dialog boxes. In the dialog boxes, we can add multiple types of elements, and one of them is the dropdown component. In the example, we create a string array optionsToChoose containing the options that we want to show in the dropdown menu.

Then, we call the JOptionPane.showInputDialog() command that takes multiple arguments; first is the parent component, where we can attach the dialog to a frame like JFrame. The second argument is the message to display beside the dropdown. We can also set the dialog’s title, which is the third argument, then comes the message type, which can be anything like an ERROR_MESSAGE or a PLAIN_MESSAGE, but we use QUESTION_MESSAGE.

The next argument is the icon that we can display beside the dropdown, but we set it as null. The sixth argument is the array of options to choose in the dropdown, and the last argument is the value from the options to be chosen as default. At last, we get the selected value returned by the JOptionPane.showInputDialog() method as a string and show it in the output.

import javax.swing.*;

public class DropDown {
    public static void main(String[] args) {
        String[] optionsToChoose = {"Apple", "Orange", "Banana", "Pineapple", "None of the listed"};

        String getFavFruit = (String) JOptionPane.showInputDialog(
                null,
                "What fruit do you like the most?",
                "Choose Fruit",
                JOptionPane.QUESTION_MESSAGE,
                null,
                optionsToChoose,
                optionsToChoose[3]);

        System.out.println("Your chosen fruit: " + getFavFruit);
    }
}

Output:

Java drop down menu

Your chosen fruit: Apple

Create a Dropdown Menu Using JComboBox in Java

In this example, we use the JComboBox function, which is a part of the javax.swing package and is used to show a dropdown list in an interface. Below, we first create the array of options to display in the dropdown list. JComboBox is a component and needs a frame to reside, so we create a JFrame object. Then, we create the JComboBox object and pass the options array as its argument in the constructor.

We set the position and the size of the dialog box using the jComboBox.setBounds() function. Then we make a JButton object, pass the text to show on it inside the constructor, and set the bounds. Finally, to show a message when an option is chosen from the dropdown, we create a JLabel and set its bounds.

Next, we add all components in the JFrame using jFrame.add(). We set the layout of jFrame as null and fix its size and visibility. At the end of the code, we also add an ActionListener command that listens to the action performed by the button and calls its method actionPerformed() to show the message in the JLabel with the option that we chose.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DropDown {
    public static void main(String[] args) {
        String[] optionsToChoose = {"Apple", "Orange", "Banana", "Pineapple", "None of the listed"};

        JFrame jFrame = new JFrame();

        JComboBox<String> jComboBox = new JComboBox<>(optionsToChoose);
        jComboBox.setBounds(80, 50, 140, 20);

        JButton jButton = new JButton("Done");
        jButton.setBounds(100, 100, 90, 20);

        JLabel jLabel = new JLabel();
        jLabel.setBounds(90, 100, 400, 100);

        jFrame.add(jButton);
        jFrame.add(jComboBox);
        jFrame.add(jLabel);
        
        jFrame.setLayout(null);
        jFrame.setSize(350, 250);
        jFrame.setVisible(true);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedFruit = "You selected " + jComboBox.getItemAt(jComboBox.getSelectedIndex());
                jLabel.setText(selectedFruit);
            }
        });

    }
}

Output:

Java drop down menu 2

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java GUI

  • Draw a Line in Java
  • Draw a Triangle in Java