?
Solved

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

Posted on 2005-03-10
15
Medium Priority
?
608 Views
Last Modified: 2012-05-05
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
Comment
Question by:tbarrawi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 4
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13510421
>>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
 
LVL 92

Expert Comment

by:objects
ID: 13510519
Use a Map of ListModel's.
0
 

Author Comment

by:tbarrawi
ID: 13511580
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13511604
You'll have to make all queries ahead of time
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 240 total points
ID: 13511639
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
 
LVL 92

Accepted Solution

by:
objects earned 260 total points
ID: 13511678
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13511686
Or

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


then

String key = (String)combo1.getSelectedItem();
combo2.setModel((ComboBoxModel)map.get(new Integer(key)));
0
 
LVL 92

Expert Comment

by:objects
ID: 13511708
> map.put(new Integer(1), new DefaultComboBoxModel(v1));

already suggested
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13511746
>>already suggested

ROFL
0
 

Author Comment

by:tbarrawi
ID: 13511844
Thanks for the prompt reply. I have splitted the points to be fair
0
 

Author Comment

by:tbarrawi
ID: 13511853
btw, whats ROFL...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13511854
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13511889
0
 
LVL 92

Expert Comment

by:objects
ID: 13511976
Thank mate, let me know if you need any further assistance.
0
 

Author Comment

by:tbarrawi
ID: 13511995
ROFLASTC :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question