Jun 23, 2026

How to create a combo box in Swing?

Leave a message

Creating a combo box in Swing is a fundamental skill for Java developers aiming to build interactive and user - friendly graphical user interfaces (GUIs). As a Swing supplier, we understand the importance of providing high - quality components and the knowledge to use them effectively. In this blog, we'll walk you through the process of creating a combo box in Swing, from the basic setup to more advanced customizations.

Basic Setup of a Combo Box in Swing

To start with, you need to have a basic understanding of Java and the Swing library. Swing is a set of GUI components for Java, and it provides a wide range of widgets, including combo boxes.

First, you need to import the necessary Swing packages. Here is a simple Java code snippet to create a basic combo box:

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

public class ComboExample {
    public static void main(String[] args) {
        // Create a JFrame
        JFrame frame = new JFrame("Combo Box Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        // Create an array of items for the combo box
        String[] items = {"Option 1", "Option 2", "Option 3"};

        // Create a JComboBox
        JComboBox<String> comboBox = new JComboBox<>(items);

        // Add the combo box to the frame
        frame.add(comboBox, BorderLayout.CENTER);

        // Make the frame visible
        frame.setVisible(true);
    }
}

In this code, we first import the javax.swing and java.awt packages. Then, we create a JFrame which is the main window of our application. We define an array of strings that will be the items in our combo box. After that, we create a JComboBox object and pass the array of items to its constructor. Finally, we add the combo box to the frame and make the frame visible.

Customizing the Combo Box

Once you have the basic combo box up and running, you may want to customize it to fit your application's needs.

Changing the Font and Color

You can change the font and color of the combo box items. Here is an example:

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

public class CustomComboExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Custom Combo Box");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        String[] items = {"Red", "Green", "Blue"};
        JComboBox<String> comboBox = new JComboBox<>(items);

        // Change the font
        comboBox.setFont(new Font("Arial", Font.BOLD, 14));

        // Change the foreground color
        comboBox.setForeground(Color.RED);

        frame.add(comboBox, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}

In this code, we use the setFont method to change the font of the combo box items, and the setForeground method to change the text color.

Adding an Event Listener

You can add an event listener to the combo box to perform an action when an item is selected. Here is an example:

Swing Link suppliersSwing Link price

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

public class ComboWithListener {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Combo Box with Listener");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        String[] items = {"Apple", "Banana", "Cherry"};
        JComboBox<String> comboBox = new JComboBox<>(items);

        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedItem = (String) comboBox.getSelectedItem();
                JOptionPane.showMessageDialog(frame, "You selected: " + selectedItem);
            }
        });

        frame.add(comboBox, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}

In this code, we add an ActionListener to the combo box. When an item is selected, the actionPerformed method is called, and a message dialog is displayed showing the selected item.

Advanced Combo Box Features

Using Custom Renderers

You can use a custom renderer to change the appearance of the combo box items. For example, you can display an image along with the text in each item.

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

class CustomRenderer extends JLabel implements ListCellRenderer<Object> {
    public CustomRenderer() {
        setOpaque(true);
    }

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        return this;
    }
}

public class CustomRendererExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Custom Renderer Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        String[] items = {"Item 1", "Item 2", "Item 3"};
        JComboBox<String> comboBox = new JComboBox<>(items);

        comboBox.setRenderer(new CustomRenderer());

        frame.add(comboBox, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}

In this code, we create a custom renderer class CustomRenderer that extends JLabel and implements ListCellRenderer. We override the getListCellRendererComponent method to customize the appearance of each item in the combo box.

Our Swing Products

As a Swing supplier, we offer a wide range of high - quality swing components. For example, we have Rubber Covered Chain which provides excellent durability and smooth operation. Our Rubber Coated Anchor Chain is perfect for applications that require strong and reliable support. And our Swing Link is a versatile accessory that can enhance the functionality of your swing setup.

Conclusion

Creating a combo box in Swing is a relatively straightforward process, but it offers a lot of room for customization. Whether you are building a simple application or a complex GUI, understanding how to create and customize combo boxes is essential. As a Swing supplier, we are committed to providing you with the best components and the knowledge to use them effectively. If you are interested in our products or have any questions about Swing components, we encourage you to contact us for a procurement discussion. We look forward to working with you to meet your Swing needs.

References

  • "Java Swing: A Beginner's Guide" by Herbert Schildt
  • Oracle Java Documentation on Swing Components
Send Inquiry