aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Multiple combo boxs - one ItemListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Multiple combo boxs - one ItemListener" Watch "Multiple combo boxs - one ItemListener" New topic
Author

Multiple combo boxs - one ItemListener

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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?


OCPJP 6, OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

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


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Solution 2 worked a treat. Thanks Roel.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple combo boxs - one ItemListener