Hi Frnds,
I have one question, i.e., i have a form with 2 Dropdown boxes.
In that one should be static dropdown and another should be dynamic dropdown.
while loading the page i am able to get the data for both the dropdowns.
But my question is when we select any thing on first dropdown then the second one will display all the related list based on the first dropdown value.
For example:
The data in the DB is like this
1-A, 1-B,1-C,1-D;
2-M,2-N,2-O;
3-X,3-Y,3-Z and so on.
The first dropdown should display 1,2,3,4 and second one should display the corresponding values on it.
Suppose if u select 1, then the second dropdown display A,B,C,D.
If u select 2,then second dropdown display M,N,O and so on.
Plz help me.. This is very urgent need for me.
Thanks in advance..
Thanks,
- Sai
03-16-2006, 03:54 PM
xtsunami
Here's some code that includes what you're looking for. The significant part is the SecondaryListModel.updateContents(). Of course, you will need to pick and update the code below depending on how you setup the list contents. You can run em for test purposes. Good luck!
public Program()
{
JFrame frame = new JFrame("Drop-down lists");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
multiLevelMap = new HashMap();
String[] dropDownAItems = new String[] { "1", "2", "3" };
multiLevelMap.put(dropDownAItems[0], new String[] {"A","B","C","D"});
multiLevelMap.put(dropDownAItems[1], new String[] {"M","N","O"});
multiLevelMap.put(dropDownAItems[2], new String[] {"X","Y","Z"});
dropDownA = new JComboBox(dropDownAItems);
dropDownBModel = new SecondaryListModel((Object[])multiLevelMap.get("1"));
dropDownB = new JComboBox(dropDownBModel);
dropDownA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] newContents = (Object[]) multiLevelMap.get(dropDownA.getSelectedItem());
dropDownBModel.updateContents(newContents);
}
});