How do I add items and remove items from JComboBox?

This example demonstrate how to add an items into a specific position in the combo box list or at the end of the list using the insertItemAt(Object o, int index) and addItem(Object o) method. In the code we also learn how to remove one or all items from the list using removeItemAt(int index) method and removeAllItems() method of JComboBox.

package org.kodejava.example.swing;

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

public class ComboBoxAddRemoveItem extends JFrame {
    public ComboBoxAddRemoveItem() {

    private void initialize() {
        setSize(300, 300);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        String[] items = new String[] {"Two", "Four", "Six", "Eight"};
        final JComboBox comboBox = new JComboBox(items);


        JButton button1 = new JButton("Add One");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Add item "One" at the beginning of the list.
                comboBox.insertItemAt("One", 0);
        JButton button2 = new JButton("Add Five and Nine");
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Add item Five on the third index and Nine at the end of the
                // list.
                comboBox.insertItemAt("Five", 3);


        JButton remove1 = new JButton("Remove Eight and Last");
        remove1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Remove the Eight item and the last item from the list.
                comboBox.removeItemAt(comboBox.getItemCount() - 1);
        JButton remove2 = new JButton("Remove All");
        remove2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {


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

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳


Leave a Reply

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