How do I get items of JComboBox?

This example demonstrate the use of JComboBox‘s getItemCount() method and getItemAt(int index) to get the number of items inside the combo box and how to obtain each of them.

package org.kodejava.swing;

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

public class ComboBoxGetItems extends JFrame {
    public ComboBoxGetItems() {
        initialize();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ComboBoxGetItems().setVisible(true));
    }

    private void initialize() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Create JComboBox with grade as the values;
        String[] items = new String[]{"A", "B", "C", "D", "E", "F"};
        final JComboBox<String> gradeCombo = new JComboBox<>(items);
        getContentPane().add(gradeCombo);

        JButton button = new JButton("Get Items");
        final JLabel label = new JLabel("Items count: ");

        button.addActionListener(e -> {
            // Get the number of items in the combo box drop-down. Iterate
            // from zero to the number of combo box items and get each item
            // of the specified index.
            //
            // In this example we just place the items inside a StringBuilder
            // and print it later on.
            int count = gradeCombo.getItemCount();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < count; i++) {
                builder.append(gradeCombo.getItemAt(i));
                if (i < count - 1) {
                    builder.append(", ");
                }
            }
            label.setText("Item count: " + count + "; ["
                    + builder + "]");
        });

        getContentPane().add(button);
        getContentPane().add(label);
    }
}

How do I set and get the selected item in JComboBox?

The code below demonstrate how to set the selected item of JComboBox and then on how to get the value of the selected item. In this example we set the JComboBox component so that user can enter their own value.

package org.kodejava.swing;

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

public class ComboBoxSelectedItem extends JFrame {
    public ComboBoxSelectedItem() {
        initialize();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new ComboBoxSelectedItem().setVisible(true));
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JComboBox Selected Item");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Create a combo box with four items and set it to editable so that 
        // user can
        // enter their own value.
        final JComboBox<String> comboBox = 
                new JComboBox<>(new String[]{"One", "Two", "Three", "Four"});
        comboBox.setEditable(true);
        getContentPane().add(comboBox);

        // Create two button that will set the selected item of the combo box. 
        // The first button select "Two" and second button select "Four".
        JButton button1 = new JButton("Set Two");
        getContentPane().add(button1);
        button1.addActionListener(e -> comboBox.setSelectedItem("Two"));

        JButton button2 = new JButton("Set Four");
        getContentPane().add(button2);
        button2.addActionListener(e -> comboBox.setSelectedItem("Four"));

        // Create a text field for displaying the selected item when we press 
        // the "Get Value" button. When user enter their own value the selected 
        // item returned is the string that entered by user.
        final JTextField textField = new JTextField("");
        textField.setPreferredSize(new Dimension(150, 20));

        JButton button3 = new JButton("Get Value");
        getContentPane().add(button3);
        getContentPane().add(textField);
        button3.addActionListener(
                e -> textField.setText((String) comboBox.getSelectedItem()));
    }
}

How do I create JComboBox?

A JComboBox allows user to select a value from a drop-down items available in the component. When the combo box set to editable user can enter their own value by typing it directly in the combo box editor. The code below demonstrate how you can create a simple combo box component.

package org.kodejava.swing;

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

public class ComboBoxCreate extends JFrame {
    public ComboBoxCreate() {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JComboBox Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        JLabel label1 = new JLabel("Month  :");
        JLabel label2 = new JLabel("Number :");

        // Create some items for our JComboBox component. In this example we are
        // going to pass an array of string which are the name of the month.
        String[] months = {"January", "February", "March", "April", "Mei", "June",
                "July", "August", "September", "October", "November", "December"};

        // Create a month selection combo box.
        JComboBox<String> comboBox = new JComboBox<>(months);

        // Below, instead of passing directly a string array we create a ComboBoxModel
        // as the combo box data model. Using a model we can for example define the
        // selected item of the combo box.
        ComboBoxModel<String> model =
                new DefaultComboBoxModel<>(new String[]{"1", "2", "3", "4", "5"});
        model.setSelectedItem("3");
        JComboBox<String> numberComboBox = new JComboBox<>(model);

        // We also set the combo box to be editable so that user can enter their own
        // value other that those defined in the combo box.
        numberComboBox.setEditable(true);

        // Add the entire component to out frame content pane.
        getContentPane().add(label1);
        getContentPane().add(comboBox);

        getContentPane().add(label2);
        getContentPane().add(numberComboBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ComboBoxCreate().setVisible(true));
    }
}