JComboBox Selection Change Listener?

363,312

Solution 1

It should respond to ActionListeners, like this:

combo.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        doSomething();
    }
});

@John Calsbeek rightly points out that addItemListener() will work, too. You may get 2 ItemEvents, though, one for the deselection of the previously selected item, and another for the selection of the new item. Just don't use both event types!

Solution 2

Code example of ItemListener implementation

class ItemChangeListener implements ItemListener{
    @Override
    public void itemStateChanged(ItemEvent event) {
       if (event.getStateChange() == ItemEvent.SELECTED) {
          Object item = event.getItem();
          // do something with object
       }
    }       
}

Now we will get only selected item.

Then just add listener to your JComboBox

addItemListener(new ItemChangeListener());

Solution 3

I would try the itemStateChanged() method of the ItemListener interface if jodonnell's solution fails.

Solution 4

Here is creating a ComboBox adding a listener for item selection change:

JComboBox comboBox = new JComboBox();

comboBox.setBounds(84, 45, 150, 20);
contentPane.add(comboBox);

JComboBox comboBox_1 = new JComboBox();
comboBox_1.setBounds(84, 97, 150, 20);
contentPane.add(comboBox_1);
comboBox.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent arg0) {
        //Do Something
    }
});

Solution 5

You may try these

 int selectedIndex = myComboBox.getSelectedIndex();

-or-

Object selectedObject = myComboBox.getSelectedItem();

-or-

String selectedValue = myComboBox.getSelectedValue().toString();
Share:
363,312

Related videos on Youtube

Allain Lalonde
Author by

Allain Lalonde

I'm a Software Developer working on Alternative User Interfaces for business projects. I love working on things that make you "Why?".

Updated on August 18, 2021

Comments

  • Allain Lalonde
    Allain Lalonde almost 3 years

    I'm trying to get an event to fire whenever a choice is made from a JComboBox.

    The problem I'm having is that there is no obvious addSelectionListener() method.

    I've tried to use actionPerformed(), but it never fires.

    Short of overriding the model for the JComboBox, I'm out of ideas.

    How do I get notified of a selection change on a JComboBox?**

    Edit: I have to apologize. It turns out I was using a misbehaving subclass of JComboBox, but I'll leave the question up since your answer is good.

    • user1568901
      user1568901 almost 11 years
      Excellent question, since I've noticed that actionPerformed fires on loading a combobox, but NOT on selecting when one would expect it.
  • Dan Dyer
    Dan Dyer almost 16 years
    I'd prefer ItemListener (just make sure to check the ItemEvent to see whether it is a selection or deselection even). The ActionListener can be fired even if the selection hasn't changed (i.e. if the user clicks on the already selected item). This may or may not be what you want.
  • user1568901
    user1568901 almost 11 years
    I know why this was downvoted, but I believe those downvotes should be reconsidered... If you check the selected index in the actionPerformed method, you'll find that this works. It does fire, and excluding -1 events will allow you to exclude the false firing on start. Solved my problem!
  • kleopatra
    kleopatra almost 11 years
    this answers enhances previous answers in that ... ? Please don't duplicate! Unrelated: never-ever do any manual sizing/locating of components, that's the exclusive task of a suitable LayoutManager
  • Gabriel Câmara
    Gabriel Câmara over 9 years
    I know this question is old, but in order to complement, ActionListener is also fired if addItem method is called.
  • dmark
    dmark over 8 years
    I tried this code, one problem is that when I click the item that is already selected, the listener will not be triggered, because there is no state change.
  • Viacheslav
    Viacheslav over 8 years
    @dmark I think this is reasonable, cause nothing was changed. Maybe you should reorganize your actions somehow to not depend on such scenario.
  • Tia
    Tia over 7 years
    @dmark Thanks, your comment helped me solve a ques :)
  • Orwellophile
    Orwellophile almost 4 years
    A useful answer, if only as a quick reference to the appropriate methods for JComboBox.
  • Hiran Chaudhuri
    Hiran Chaudhuri about 2 years
    This is not a useful answer. If there is no event that gets fired you'd have to poll for values. As such this is unrelated to the question.