<p>I'm trying to get an event to fire whenever a choice is made from a <code>JComboBox</code>.</p>

<p>The problem I'm having is that there is no obvious <code>addSelectionListener()</code> method.</p>

<p>I've tried to use <code>actionPerformed()</code>, but it never fires.</p>

<p>Short of overriding the model for the <code>JComboBox</code>, I'm out of ideas.</p>

<p>How do I get notified of a selection change on a <code>JComboBox</code>?**</p>

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

[{'answer_id': 58963, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 9, 'selected': True, 'text': '<p>It should respond to <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html" rel="noreferrer">ActionListeners</a>, like this:</p>\n\n<pre><code>combo.addActionListener (new ActionListener () {\n    public void actionPerformed(ActionEvent e) {\n        doSomething();\n    }\n});\n</code></pre>\n\n<p><a href="https://stackoverflow.com/a/58965/1429387">@John Calsbeek</a> rightly points out that <code>addItemListener()</code> will work, too.  You may get 2 <code>ItemEvents</code>, 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!</p>\n'}, {'answer_id': 58965, 'author': 'John Calsbeek', 'author_id': 5696, 'author_profile': 'https://Stackoverflow.com/users/5696', 'pm_score': 5, 'selected': False, 'text': '<p>I would try the <code>itemStateChanged()</code> method of the <a href="http://java.sun.com/javase/6/docs/api/java/awt/event/ItemListener.html" rel="noreferrer"><code>ItemListener</code></a> interface if jodonnell\'s solution fails.</p>\n'}, {'answer_id': 2187058, 'author': 'JavaKeith', 'author_id': 264662, 'author_profile': 'https://Stackoverflow.com/users/264662', 'pm_score': 3, 'selected': False, 'text': '<p>You may try these</p>\n\n<pre><code> int selectedIndex = myComboBox.getSelectedIndex();\n</code></pre>\n\n<p>-or-</p>\n\n<pre><code>Object selectedObject = myComboBox.getSelectedItem();\n</code></pre>\n\n<p>-or-</p>\n\n<pre><code>String selectedValue = myComboBox.getSelectedValue().toString();\n</code></pre>\n'}, {'answer_id': 14424530, 'author': 'Viacheslav', 'author_id': 1043067, 'author_profile': 'https://Stackoverflow.com/users/1043067', 'pm_score': 7, 'selected': False, 'text': '<p>Code example of <code>ItemListener</code> implementation</p>\n\n<pre><code>class ItemChangeListener implements ItemListener{\n    @Override\n    public void itemStateChanged(ItemEvent event) {\n       if (event.getStateChange() == ItemEvent.SELECTED) {\n          Object item = event.getItem();\n          // do something with object\n       }\n    }       \n}\n</code></pre>\n\n<p>Now we will get only selected item.</p>\n\n<p>Then just add listener to your JComboBox</p>\n\n<pre><code>addItemListener(new ItemChangeListener());\n</code></pre>\n'}, {'answer_id': 14667156, 'author': 'Craig Wayne', 'author_id': 1654250, 'author_profile': 'https://Stackoverflow.com/users/1654250', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p><a href="https://stackoverflow.com/questions/14647439/how-to-get-previous-last-item-jcombobox">How do I get the previous or last item?</a></p>\n'}, {'answer_id': 17846338, 'author': 'Ahuramazda', 'author_id': 2581460, 'author_profile': 'https://Stackoverflow.com/users/2581460', 'pm_score': 4, 'selected': False, 'text': '<p>Here is creating a ComboBox adding a listener for item selection change:</p>\n\n<pre class="lang-java prettyprint-override"><code>JComboBox comboBox = new JComboBox();\n\ncomboBox.setBounds(84, 45, 150, 20);\ncontentPane.add(comboBox);\n\nJComboBox comboBox_1 = new JComboBox();\ncomboBox_1.setBounds(84, 97, 150, 20);\ncontentPane.add(comboBox_1);\ncomboBox.addItemListener(new ItemListener() {\n    public void itemStateChanged(ItemEvent arg0) {\n        //Do Something\n    }\n});\n</code></pre>\n'}, {'answer_id': 64692212, 'author': 'Mehmet Onar', 'author_id': 6573672, 'author_profile': 'https://Stackoverflow.com/users/6573672', 'pm_score': 2, 'selected': False, 'text': '<p>you can do this with jdk &gt;= 8</p>\n<pre><code>getComboBox().addItemListener(this::comboBoxitemStateChanged);\n</code></pre>\n<p>so</p>\n<pre><code>public void comboBoxitemStateChanged(ItemEvent e) {\n    if (e.getStateChange() == ItemEvent.SELECTED) {\n        YourObject selectedItem = (YourObject) e.getItem();\n        //TODO your actitons\n    }\n}\n</code></pre>\n'}, {'answer_id': 68821546, 'author': 'EverCpp', 'author_id': 12048805, 'author_profile': 'https://Stackoverflow.com/users/12048805', 'pm_score': 2, 'selected': False, 'text': '<p>I use this:</p>\n<pre><code>    cb = new JComboBox&lt;String&gt;();\n    cb.setBounds(10, 33, 46, 22);\n    panelConfig.add(cb);\n    for(int i = 0; i &lt; 10; ++i)\n    {\n        cb.addItem(Integer.toString(i));\n    }\n    cb.addItemListener(new ItemListener()\n    {\n        @Override\n        public void itemStateChanged(ItemEvent e)\n        {\n            if(e.getID() == temEvent.ITEM_STATE_CHANGED)\n            {\n                if(e.getStateChange() == ItemEvent.SELECTED)\n                {\n                    JComboBox&lt;String&gt; cb = (JComboBox&lt;String&gt;) e.getSource();\n                    String newSelection = (String) cb.getSelectedItem();\n                    System.out.println(&quot;newSelection: &quot; + newSelection);\n                }\n            }\n        }\n    });\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/58939', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2443/']