CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > JComboBox in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

JComboBox in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

JComboBox is a component in Java Swing that allows users to select an item from a drop-down list. JComboBox in Java Swing generates events that allow developers to respond to changes in the selected item.

Following are the events generated by JComboBox and their descriptions:

  1. ItemStateChanged: This event is generated when the selected item in the JComboBox changes. The event object contains information about the new and old selected items.
  2. ActionPerformed: This event is generated when the user selects an item from the JComboBox. The event object contains information about the selected item.

To use these events in our Java Swing program, we need to register event listeners to the JComboBox component.

JComboBox ItemStateChanged Example

Following is an example code that demonstrates how to use the ItemStateChanged event in JComboBox:

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JComboBoxDemo extends JFrame implements ItemListener {
	private JComboBox comboBox;
	private JLabel label;

	public JComboBoxDemo() {
		String[] items = { "Item 1", "Item 2", "Item 3" };
		comboBox = new JComboBox(items);
		comboBox.addItemListener(this);
		label = new JLabel("Please select an item.");
		setLayout(new FlowLayout());
		add(comboBox);
		add(label);
		pack();
		setVisible(true);
	}

	@Override
	public void itemStateChanged(ItemEvent event) {
		if (event.getStateChange() == ItemEvent.SELECTED) {
			String selectedItem = (String) event.getItem();
			label.setText("You selected " + selectedItem);
		}
	}

	public static void main(String[] args) {
		new JComboBoxDemo();
	}

}

Output:

JComboBox in Java Swing with itemStatechange event

In this example, we create a JFrame that contains a JComboBox and a JLabel. We register an ItemListener to the JComboBox and implement the itemStateChanged() method to handle the ItemStateChanged event. When the user selects an item from the JComboBox, the itemStateChanged() method is called and updates the JLabel with the selected item.

You can use a similar approach to handle the ActionPerformed event by registering an ActionListener to the JComboBox component.

JComboBox ActionPerformed Example

Following is an example code that demonstrates how to use the ActionPerformed event in JComboBox:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JComboBoxDemo extends JFrame implements ActionListener {
	   private JComboBox comboBox;
	   private JLabel label;
	   
	   public JComboBoxDemo() {
	      String[] items = {"Item 1", "Item 2", "Item 3"};
	      comboBox = new JComboBox(items);
	      comboBox.addActionListener(this);
	      label = new JLabel("Please select an item.");
	      setLayout(new FlowLayout());
	      add(comboBox);
	      add(label);
	      pack();
	      setVisible(true);
	   }
	   
	   public void actionPerformed(ActionEvent event) {
	      String selectedItem = (String) comboBox.getSelectedItem();
	      label.setText("You selected " + selectedItem);
	   }
	   
	   public static void main(String[] args) {
	      new JComboBoxDemo();
	   }

}

The output of this code is the same as the above code.

Reference: https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

Related Posts:

  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • How to Write User Stories and Acceptance Criteria: A…
  • MySQL Commands for Developers
  • Control Statements in Java
  • What is Java? Exploring Its Key Features, Benefits,…
  • Install and Set Up Java Development Environment
Tags:javaswing
Was this article helpful?
← Previous ArticleCompoundBorder in Java Swing
Next Article →Wrapper Classes in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap