• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

two JComboBox database driven. The value of the second combobox depends on the selected value of first.

Hi experts, what's the smart way to do this :

the first JCombobox1 is populated on application load with values from table1. The second JCombobox2 should be populate from table2 based on the selected value of JCombobox1.  Do I run a database query and repopulate JCombobox2 with the correct values everytime JCombobox1 selected value change? or I play with Vectors [ a code sample for a Vector approach solution will be nice]
The data in both tables is few hundred row, not huge tables.  Thanks!
0
tbarrawi
Asked:
tbarrawi
  • 7
  • 4
  • 4
2 Solutions
 
CEHJCommented:
>>Do I run a database query ... or I play with Vectors

If you can hold it comfortably in memory you can use a Map of Lists
0
 
objectsCommented:
Use a Map of ListModel's.
0
 
tbarrawiAuthor Commented:
objects or CEHJ,  I will have to create List Models for all the available options?    

Can you please give me a simple example so I understand better how it is done? [lets say  combo box1 has OptionA, OptionB and combo box2  will show   OptionA1, OptionA2 or OptionB1, OptionB2  depending on what is selected in combo box1]
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
You'll have to make all queries ahead of time
0
 
CEHJCommented:
select x from table where y = 1

select x from table where y = 2

etc.

Put ResultSet into a Vector, then the Map:

map.put(new Integer(1), v1);
map.put(new Integer(2), v2);


You can then do what you like with the Vector, including creating a DefaultComboBoxModel
0
 
objectsCommented:
map.put("Option A", listModelForA);
map.put("Option B", listModelForB);

then when an item is selected

ListModel model = (ListModel) map.get(combo1.getSelectedValue());
combo2.setModel(model);

0
 
CEHJCommented:
Or

map.put(new Integer(1), new DefaultComboBoxModel(v1));


then

String key = (String)combo1.getSelectedItem();
combo2.setModel((ComboBoxModel)map.get(new Integer(key)));
0
 
objectsCommented:
> map.put(new Integer(1), new DefaultComboBoxModel(v1));

already suggested
0
 
CEHJCommented:
>>already suggested

ROFL
0
 
tbarrawiAuthor Commented:
Thanks for the prompt reply. I have splitted the points to be fair
0
 
tbarrawiAuthor Commented:
btw, whats ROFL...
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
Thank mate, let me know if you need any further assistance.
0
 
tbarrawiAuthor Commented:
ROFLASTC :)
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 7
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now