JComboBox - how to add a listener

I want to run a method when a new item is selected from the JComboBox.  I beleive I use the selectedItemChanged() method but not sure how.  How do I set up the listener to do this?  

Thanks
LVL 18
mgcITAsked:
Who is Participating?
 
objectsCommented:
combo.addItemListener(new ItemListener()
{
   public void       itemStateChanged(ItemEvent e)
   {
      // selection has changed

      if (e.getStateChange()==ItemEvent.SELECTED)
      {
          Object selected = e.getItem();
      }
   }
});
0
 
CEHJCommented:
A plain ActionListener can be added to the combo and in the handler you  can call getSelectedItem
0
 
mgcITAuthor Commented:
in this example would selected = the actual item that was selected?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
mgcITAuthor Commented:
>> Object selected = e.getItem();

I'm referring to this.  
0
 
objectsCommented:
yes
0
 
mgcITAuthor Commented:
Thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.