JComboBox in Java Swing

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