User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 422,381 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,658 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting

JComboBox

Join Date: Dec 2007
Location: Greece
Posts: 537
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 57
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Pro

Re: JComboBox

  #2  
May 16th, 2008
My swing is a little rusty but if you add an Object in a comboBox, the toString() method will be displayed. And when you try to get the selected item it will return the entire object.

So have an object Person with attributes: name, percentage.
The Person should have a public String toString() { return name; } method.
So when you add the Person john with values:
name="John"
percentage=0.15
to the JComboBox it will be display what the toString method returns: "John"
When you do combo.getSelectedItem(); NOT combo.getSelectedIndex(); you will get the object Person:
Person selectedPerson = (Person)nameCombo.getSelectedIndex();
then selectedPerson will not only have the name but the percentage as well. So you will get it:
selectedPerson.getPercentage(); and multiply with whatever you want and set it to another field.
I have never tried the above approach, but I think that it will work. Also I hope I understood correctly you problem.
I AM the 12th CYLON
Reply With Quote  
All times are GMT -4. The time now is 8:40 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC