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

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

ItemListener in Java

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

An ItemListener is used to handle item selections or changes in components like JComboBox (dropdown) or JCheckBox. It allows us to respond when a user selects or deselects an item. Following is the steps of how we can use ItemListener to handle item selections:

Import Required Libraries

Begin by importing the necessary libraries for working with Swing components and ItemListener:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

Create a JFrame

Create a JFrame (the main window) to hold our GUI components:

public class ItemSelectionDemo {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Item Selection Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setLayout(new FlowLayout());

        // Create a JComboBox with items and add ItemListener
        String[] items = {"Option 1", "Option 2", "Option 3"};
        JComboBox<String> comboBox = new JComboBox<>(items);
        comboBox.addItemListener(new MyItemListener());

        frame.add(comboBox);
        frame.setVisible(true);
    }
}

Implement ItemListener

Create a class that implements the ItemListener interface to define the action that should occur when an item is selected or deselected:

class MyItemListener implements ItemListener {
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            // Code to execute when an item is selected
            String selected = (String) e.getItem();
            JOptionPane.showMessageDialog(null, "Selected: " + selected);
        }
    }
}

Running the program will display a window with a dropdown (JComboBox) containing the specified options. When we select an option from the dropdown, the itemStateChanged method in the MyItemListener class will be triggered, and a message dialog showing the selected item will appear.

ItemListener Example in Java

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.JOptionPane;

public class ItemListenerDemo {
	public static void main(String[] args) {
        JFrame frame = new JFrame("Item Selection Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setLayout(new FlowLayout());

        // Create a JComboBox with items and add ItemListener
        String[] items = {"Option 1", "Option 2", "Option 3"};
        JComboBox<String> comboBox = new JComboBox<>(items);
        comboBox.addItemListener(new MyItemListener());

        frame.add(comboBox);
        frame.setVisible(true);
    }
}

class MyItemListener implements ItemListener {
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            // Code to execute when an item is selected
            String selected = (String) e.getItem();
            JOptionPane.showMessageDialog(null, "Selected: " + selected);
        }
    }
}

Output:

ItemListener in Java

Related Posts:

  • JCheckBox in Java Swing
  • JComboBox in Java Swing
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Event Handling in Java Swing
  • Java Swing Tutorial
  • Control Statements in Java
Tags:guijavaswing
Was this article helpful?
← Previous ArticleActionListener in Java
Next Article →Advantages of Swing Over AWT

Leave a Comment Cancel reply

You must be logged in to post a comment.

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