第二个comboBox中的选项取决于第一个comboBox

时间:2013-01-26 22:52:12

标签: java swing

  

可能重复:
  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);

我在下面尝试做的是让第二个comboBox上的选项改变,具体取决于第一个选择的内容......但它没有这样做。无论我在顶部的comboBox中选择什么,第二个选项都不会改变......我缺少什么?

            // 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);

1 个答案:

答案 0 :(得分:3)

您应该添加能够更改第二个JComboBox内容的ActionListener。此外,您应该考虑在Map中链接模型名称和术语名称。请看我的例子:

class TwoComboboxFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private Map<String, List<String>> modelNameTermName = new LinkedHashMap<String, List<String>>();

    private JComboBox<String> termNameComboBox;

    public TwoComboboxFrame() {
        init();
        buildDataModel();

        JPanel panel = createAndAddPanel();
        buildModelNameComboBox(panel);
        buildTermNameComboBox(panel);
    }

    private void buildTermNameComboBox(JPanel panel) {
        termNameComboBox = new JComboBox<String>();
        termNameComboBox.addItem("Select Model Above");
        panel.add(termNameComboBox);
    }

    private void buildModelNameComboBox(JPanel panel) {
        JComboBox<String> modelName = new JComboBox<String>();
        modelName.addItem("Choose One...");
        for (String value : modelNameTermName.keySet()) {
            modelName.addItem(value);
        }
        panel.add(modelName);
        modelName.addActionListener(new ActionListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox<String> source = (JComboBox<String>) e.getSource();
                String selectedValue = source.getSelectedItem().toString();
                List<String> termNames = modelNameTermName.get(selectedValue);
                termNameComboBox.removeAllItems();
                if (termNames == null) {
                    termNameComboBox.addItem("Select Model Above");
                } else {
                    termNameComboBox.addItem("Choose One...");
                    for (String name : termNames) {
                        termNameComboBox.addItem(name);
                    }
                }
            }
        });
    }

    private void buildDataModel() {
        modelNameTermName.put("Hypercom",
                Arrays.asList("T7Plus", "4205", "4210", "4220"));
        modelNameTermName.put("Deja Voo",
                Arrays.asList("M-3", "X-5", "X-8", "V Series"));
        //Add other data
    }

    private void init() {
        setTitle("Two comboboxex");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    }

    private JPanel createAndAddPanel() {
        JPanel panel = new JPanel(true);
        panel.setLayout(new GridLayout(1, 2));
        add(panel);
        return panel;
    }
}

使用示例:

public static void main(String[] args) throws Exception {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            TwoComboboxFrame ex = new TwoComboboxFrame();
            ex.setVisible(true);
        }
    });
}