JComboBox - how to add a listener

Posted on 2006-06-19
Last Modified: 2010-03-31
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?  

Question by:mgcIT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

Expert Comment

ID: 16938133
A plain ActionListener can be added to the combo and in the handler you  can call getSelectedItem
LVL 92

Accepted Solution

objects earned 500 total points
ID: 16938303
combo.addItemListener(new ItemListener()
   public void       itemStateChanged(ItemEvent e)
      // selection has changed

      if (e.getStateChange()==ItemEvent.SELECTED)
          Object selected = e.getItem();
LVL 18

Author Comment

ID: 16938611
in this example would selected = the actual item that was selected?
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

LVL 18

Author Comment

ID: 16938612
>> Object selected = e.getItem();

I'm referring to this.  
LVL 92

Expert Comment

ID: 16938793
LVL 18

Author Comment

ID: 16938893
Thanks for the help.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
null output 3 59
ejb mdb examples 1 39
Netbeans and org.apache.commons.lang3 issue 3 53
Java List 4 76
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question