File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes differentiate between combo box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "differentiate between combo box" Watch "differentiate between combo box" New topic
Author

differentiate between combo box

Ar vinoth
Greenhorn

Joined: Jun 24, 2009
Posts: 2
hi friends,

i am using two combo box in same program and i am adding both to itemListener class. when the user selects the the item in combo box i need to know from which combo box he is selecting.. based on that i will perform some action. I got struck here. Help me.

bye,
vino.


vino
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2563
    
    9

Welcome to the Ranch.

About your question, you need 2 item listeners, one listener for each combo box. If you don't whatever thing you do with the first combo box is also done for the 2nd combo box... and you don't want that.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
never mind, ignore this...
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
The ItemEvent class inherits a method "getSource()" from the EventObject. Try using that.

Regards


Experience and talent are independent of age
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

K. Tsang wrote:Welcome to the Ranch.

About your question, you need 2 item listeners, one listener for each combo box. If you don't whatever thing you do with the first combo box is also done for the 2nd combo box... and you don't want that.


That's not entirely true. You can use the same listener for multiple controls. There are a few ways to determine which one triggered the event.

WARNING: untested code being shown.





In the latter example, it assumes you set the name of each control when constructing your combo boxes.


GenRocket - Experts at Building Test Data
Mark O'Neal
Greenhorn

Joined: Jul 23, 2009
Posts: 5
Hi Gregg.

You can do something like this.


Hope that helps.

Mark.....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Apart from the fact that you'll need to make the combo boxes either final or instance references, I'll put on my Campbell cap:
Don't use == true or == false; simply use or


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Rob Prime wrote:I'll put on my Campbell cap:
I'm sure I say more things than that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: differentiate between combo box