• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interdependent JComboBoxes  RSS feed

 
Rico Strydom
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

We al know that there are more than one way to achieve something in programming but not all ways are good or elegant.

I am looking for the best wayto to create JComboboxes in my application that are dependend on each other's selection. E.g. If value 1 from comboboxA was selected then this selection will again play a role in the values that will be displayed in comboboxB. So in essence the comboxes act as filters for data that will be displayed in a table.

Hope you can help.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is one way:

 
Rico Strydom
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
Thanks for the reply. Unfortunately this approach is not going to work for what I want. In your example you hard code the items and the subitems. In my case I read values from a database which make the items and subitems very dynamic.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example is hardcoded because you can't post database code in a forum and make the code executable.

However, the concept of changing the ComboBoxModel in the actionPerformed() method will still apply in your situation. How you get the data is up to you. You can add the SQL in the actionPerformed() method to make it dynamic or you can hardcode the data. Or you can do a combination of both, that is the first time an item is selected you query the database and then store the item/model in a Hashtable to that the second time you request those values you don't need to access the database again which will make the code faster (but take more memory). The choice is yours.

Take time to understand what the basic approach is suggesting, then you will be able to customize it for your needs. Rarely will you ever find code that you can copy line for line, it is the approach that is important.
 
Rico Strydom
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I will let it sink in
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!