This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 5126
    
  12

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
 
Similar Threads
How.....change combobox1 value ....combobox2 contain value related to that....
combobox query
Need to update HTML form field from an applet
JComboBox problem
ActionListener on JComboBox