JComboBox Selection Change Listener?

JavaSwingJcomboboxItemlistener

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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

Solution 4 - Java

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 - Java

You may try these

 int selectedIndex = myComboBox.getSelectedIndex();

-or-

Object selectedObject = myComboBox.getSelectedItem();

-or-

String selectedValue = myComboBox.getSelectedValue().toString();

Solution 6 - Java

I was recently looking for this very same solution and managed to find a simple one without assigning specific variables for the last selected item and the new selected item. And this question, although very helpful, didn't provide the solution I needed. This solved my problem, I hope it solves yours and others. Thanks.

https://stackoverflow.com/questions/14647439/how-to-get-previous-last-item-jcombobox

Solution 7 - Java

you can do this with jdk >= 8

getComboBox().addItemListener(this::comboBoxitemStateChanged);

so

public void comboBoxitemStateChanged(ItemEvent e) {
	if (e.getStateChange() == ItemEvent.SELECTED) {
		YourObject selectedItem = (YourObject) e.getItem();
		//TODO your actitons
	}
}

Solution 8 - Java

I use this:

	cb = new JComboBox<String>();
	cb.setBounds(10, 33, 46, 22);
	panelConfig.add(cb);
	for(int i = 0; i < 10; ++i)
	{
		cb.addItem(Integer.toString(i));
	}
	cb.addItemListener(new ItemListener()
	{
		@Override
		public void itemStateChanged(ItemEvent e)
		{
			if(e.getID() == temEvent.ITEM_STATE_CHANGED)
			{
				if(e.getStateChange() == ItemEvent.SELECTED)
				{
					JComboBox<String> cb = (JComboBox<String>) e.getSource();
					String newSelection = (String) cb.getSelectedItem();
					System.out.println("newSelection: " + newSelection);
				}
			}
		}
	});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAllain LalondeView Question on Stackoverflow
Solution 1 - JavajodonnellView Answer on Stackoverflow
Solution 2 - JavaViacheslavView Answer on Stackoverflow
Solution 3 - JavaJohn CalsbeekView Answer on Stackoverflow
Solution 4 - JavaAhuramazdaView Answer on Stackoverflow
Solution 5 - JavaJavaKeithView Answer on Stackoverflow
Solution 6 - JavaCraig WayneView Answer on Stackoverflow
Solution 7 - JavaMehmet OnarView Answer on Stackoverflow
Solution 8 - JavaEverCppView Answer on Stackoverflow