• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple combo boxs - one ItemListener

 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have two combo boxs, I want to add a separate itemListener to each one so that a different method is called depending on which comboBox the user effects.

Is this possible?

Alternatively, in the itemStateChanged method, is there a way to find the identity of the calling combobox?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question and your title seems contradictory to me. In your title it seems you want multiple comboboxes and 1 ItemListener. But your question starts with "I have two combo boxs, I want to add a separate itemListener to each one"

Nonetheless both are possible. And if you want to identify the calling combobox, you have to choices:
1/ call getItem() on the ItemEvent instance of the itemStateChanged() method (a cast will be required)
2/ make a custom ComboItemListener class (which of course implements ItemListener and performs your business logic) and just pass a ComboBox to its constructor as an argument
 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solution 2 worked a treat. Thanks Roel.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic