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.example.swing;

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

public class ComboBoxSelectedItem extends JFrame {
    public ComboBoxSelectedItem() {

    private void initialize() {
        setSize(300, 300);
        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"});

        // Create two button that will set the selected item of the combo box. The
        // first button select "Two" and and second button select "Four".
        JButton button1 = new JButton("Set Two");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

        JButton button2 = new JButton("Set Four");
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

        // 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");
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.setText((String) comboBox.getSelectedItem());

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ComboBoxSelectedItem().setVisible(true);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.