// 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);
我在下面要做的是让第二个组合框的选项根据第一个选择的内容而改变......但它没有这样做。无论我在顶部组合框中选择什么,第二个选项都不会改变......我错过了什么?
// 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);
更新
我更新了代码,但现在我得到了一个 nullPoint 异常错误......这是它的位置:
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);