当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Options in second comboBox dependent on first comboBox

问题描述:

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

网友答案:

You should add ActionListener which is able to changing content of second JComboBox. Additionally, you should think about linking model names and term names in Map. Please, see my example:

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;
    }
}

Example of usage:

public static void main(String[] args) throws Exception {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            TwoComboboxFrame ex = new TwoComboboxFrame();
            ex.setVisible(true);
        }
    });
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: