0

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);
Community
  • 1
  • 1
Psest328
  • 6,575
  • 11
  • 55
  • 90
  • 2
    Also see [How to control a combo box by using another combo box swing](http://stackoverflow.com/q/10367834/1048330) – tenorsax Jan 26 '13 at 22:56
  • 3
    that did it, thank you so much. can you repost that as an answer so I can give you the credit? – Psest328 Jan 26 '13 at 23:31

1 Answers1

3

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);
        }
    });
}
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • How do you get JComboBox as generic? I tried this and it complanied the object is not generic. – ziggy Jul 05 '14 at 11:18
  • Which version of Java are you using? See documentation: http://docs.oracle.com/javase/8/docs/api/javax/swing/JComboBox.html this is a generic class. – Michał Ziober Jul 05 '14 at 22:58
  • I am using jdk 1.6. I must be doing something else wrong. Will check again. Thanks – ziggy Jul 06 '14 at 11:46