Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

populate a combobox  RSS feed

 
Marie Mazerolle
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a small app that displays a list of companies in a combobox. Basically, when the user selects a company, the company details should be displayed.
What is the best way to do this? I've thought of a few options, but I'm not sure which one is best. If I were doing it with PHP or ASP, I would query the database everytime the user selects a new company. Is this how I should be doing it in Java too? If it is, then how do I insert both the company id and the company name in my combobox? I can populate my combobox using the DefaultComboBoxModel, but so far I can only insert the company names.
Should I instead populate my combobox with "Company" objects? (I have about 2000 companies in my db).
Thanks,
Marie
 
Marie Mazerolle
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for those interested. I found the solution on Sun's discussion forums for developpers:
You can add any object with toString() to the ComboBox. So create somthing like this,

JComboBox aBox = new JComboBox();aBox.addItem( new IDName( "A", "Apple"));aBox.addItem( new IDName( "B", "Boy"));aBox.addItem( new IDName( "C", "Cat"));....String id =((IDName) aBox.getSelectedItem()).getID();.....class IDName{String id;String name;public IDName(String id_, String name_){id = id_;name = name_;}public String toString();return name;}public String getID(){return ID;}}

In the above, the value of ID would be A or B or C
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!