I've built a combobox that is dynamically populated depending on the contents of another combobox, and so on. I've decided, although it's a bit terrible, to experiment with iterating through the contents of the source array when populating the target combobox. However, although this results in combobox contents, they are repeated. I've stepped through the code, and the array is only being iterated through once.
private JComboBox regBuildingSelectBox;
...
String[] siteSelectStrings = {"Site", "London", "Long Island"};
JComboBox regSiteSelectBox = new JComboBox(siteSelectStrings);
regSiteSelectBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
getDropDownVariables gddv = new getDropDownVariables();
for(String s:
gddv.buildingSelectList
(regSiteSelectBox.getSelectedItem().toString()))
{
regBuildingSelectBox.addItem(s);
}
}
});
regSiteSelectBox.setBounds(24, 336, 282, 20);
contentPane.add(regSiteSelectBox);
regBuildingSelectBox = new JComboBox();
regBuildingSelectBox.setBounds(24, 367, 282, 20);
contentPane.add(regBuildingSelectBox);
The code containing the arrays is as follows:
public class getDropDownVariables {
public String[] buildingSelectList(String site)
{
switch (site)
{
case "London":
return new String[] {"Building", "Harvell",
"LYNX Complex", "Caroline", "Salters"};
case "Long Island":
return new String[] {"Building", "Phillips", "Pascal"};
}
return new String[] {"Failed to populate buildings"};
}
And the result: