ItemListener in Java

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

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments