Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make two JComboboxe's to be in sync when one selects an item from the Dropdownlist

 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically I would like to know how to to the following Java. Example... Say I have one JComboBox, and it is populated with 10 items which reads from a text file ("apple","banana","kiwi","orange","grape fruit"), and the other JComboBox holds the price ($1,$2,$4, etc etc...you get the drift).

How do I make in such a way when I select for example banana from the JCombobox that it syncs the other JCombox to the correct price?

Basically syncing the two JCombobox so that you do not need to select the price yourself - it does it automatically for you.

Thanks
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you get the drift


Not really. Why would you be using two combo boxes?

By using two combo boxes you are implying that the user can select a value from either combo box. So if you select Apple, then you select whatever price you want from the second combo box.

I would think a better solution would be to use a combo box for the fruit and then use a JLabel to display the price of that fruit.

To do that you would create a custom Object and store the two properties in that object. Then you create a custom renderer to display the fruit name. When the item is selected you get the price from the Object and display it in the label.

Check out Combo Box With Custom Renderer for more information and help on this approach.

Or, instead of creating a custom Object you could create a Map that stores the fruit and its related price. Then when the item is selected you look up the price of the selected item in the Map and display the value in the label. This is probably easier as you won't need a custom renderer.

Quit cross posting: http://stackoverflow.com/questions/27999882/how-to-make-two-jcomboboxes-to-be-in-sync-when-one-selects-an-item-from-the-dro
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!