Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
115 views
in JAVA by (178k points)
How do I listen for changes to a JComboBox's selection?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

To listen for changes to a JComboBox's selection, you can add an ActionListener to the JComboBox. The ActionListener will be notified whenever the user selects a different item in the JComboBox. Here are the steps to add an ActionListener to a JComboBox:

  1. Create a JComboBox object: You can create a JComboBox object using the JComboBox class constructor. For example, to create a new JComboBox object, you can use the following code:
JComboBox<String> comboBox = new JComboBox<>();
 
  1. Add items to the JComboBox: Once you have created a JComboBox object, you can add items to it using the addItem() or addItems() method.

  2. Add an ActionListener to the JComboBox: To listen for changes to the JComboBox's selection, you can add an ActionListener to the JComboBox using the addActionListener() method. The ActionListener will be notified whenever the user selects a different item in the JComboBox. Here is an example code snippet that demonstrates how to add an ActionListener to a JComboBox:

import javax.swing.JComboBox;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ExampleJComboBox {
    public static void main(String[] args) {
        String[] fruits = {"Apple", "Banana", "Cherry", "Durian", "Elderberry"};

        JComboBox<String> comboBox = new JComboBox<>();
        comboBox.addItems(fruits);

        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox<String> comboBox = (JComboBox<String>) e.getSource();
                String selectedFruit = (String) comboBox.getSelectedItem();
                System.out.println("Selected fruit: " + selectedFruit);
            }
        });

        JFrame frame = new JFrame("Example JComboBox");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(comboBox);
        frame.pack();
        frame.setVisible(true);
    }
}
 

In this example, we create a new JComboBox object and add some items to it using the addItems() method. We then add an ActionListener to the JComboBox using an anonymous inner class. Whenever the user selects a different item in the JComboBox, the actionPerformed() method of the ActionListener is called. In the actionPerformed() method, we get the selected item from the JComboBox and print it to the console.

Note that the ActionListener can also be implemented as a separate class instead of an anonymous inner class.

Related questions

0 votes
1 answer
asked May 8, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...