Possible Duplicate:
Dynamic JComboBoxes
how to control a combo box by using another combo box swing
// model drop-down
final JComboBox modelName = new JComboBox();
modelName.addItem("Choose One...");
modelName.addItem("Hypercom");
modelName.addItem("Deja Voo");
modelName.addItem("Nurit");
modelName.addItem("Verifone");
int modelIndex = modelName.getSelectedIndex();
modelPanel.add(modelName);
What I'm trying to do below is have the options on the second comboBox change depending on what was selected in the first... but it's not doing it. No matter what I select in the top comboBox, the options for the second don't change... what am I missing?
// Terminal Panel (within Panel1)
JPanel termPanel = new P(15);
termPanel.setBackground(Color.WHITE);
panel1.add(termPanel);
final JComboBox termName = new JComboBox();
if (modelIndex == 1) {
termName.addItem("Choose One...");
termName.addItem("T7Plus");
termName.addItem("4205");
termName.addItem("4210");
termName.addItem("4220");
} else if (modelIndex == 2) {
termName.addItem("Choose One...");
termName.addItem("M-3");
termName.addItem("X-5");
termName.addItem("X-8");
termName.addItem("V Series");
} else if (modelIndex == 3) {
termName.addItem("Choose One...");
termName.addItem("2085");
termName.addItem("3020/8320");
termName.addItem("8400");
termName.addItem("8000/8020");
} else if (modelIndex == 4) {
termName.addItem("Choose One...");
termName.addItem("37 Dial");
termName.addItem("37 IP");
termName.addItem("VX Dial");
termName.addItem("VX IP");
} else {
termName.addItem("Select Model Above");
}
termName.getSelectedIndex();
termPanel.add(termName);
UPDATE
I updated the code, but now I'm getting a nullPoint Exception Error... here's where it's at:
final DefaultComboBoxModel termModel = new DefaultComboBoxModel(new String[]{"Choose One...", "Hypercom", "DejaVoo", "Nurit", "Verifone"});
...
// model drop-down
modelName.setModel(termModel); // NULLPOINT EXCEPTION HERE
modelName.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
if ("Hypercom".equals(modelName.getSelectedItem())){
termName.setModel(hSpecModel);
} else if ("Deja Voo".equals(modelName.getSelectedItem())){
termName.setModel(dSpecModel);
} else if ("Nurit".equals(modelName.getSelectedItem())){
termName.setModel(nSpecModel);
} else if ("Verifone".equals(modelName.getSelectedItem())){
termName.setModel(vSpecModel);
} else {
termName.setModel(slctAbove);
}
}
});
modelPanel.add(modelName);