JComboBox Selection Change Listener?

橙三吉。 提交于 2019-11-25 22:05:14
jodonnell

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!

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());

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

Ahuramazda

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
        }
    });
}
JavaKeith

You may try these

 int selectedIndex = myComboBox.getSelectedIndex();

-or-

Object selectedObject = myComboBox.getSelectedItem();

-or-

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

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.

How do I get the previous or last item?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!