0

Need help with A jcombobox to populate another jcombobox to populate a third jcombobox.

I can manage two with code that i found and have been playing around with 2d arrays but cant seem to get three jcomboboxes to link.

here is the code so far:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComboTest extends JPanel implements ActionListener, Runnable {

    private final JComboBox combo1 = new JComboBox(
        new String[]{"please select", "Ireland", "England"});
    private final JComboBox combo2 = new JComboBox();
    private final JComboBox combo3 = new JComboBox();
    private ComboBoxModel[] models = new ComboBoxModel[3];
    private ComboBoxModel[] models2d = new ComboBoxModel[3];


    public ComboTest() {
        models[0] = new DefaultComboBoxModel(
            new String[]{"        "});
        models[1] = new DefaultComboBoxModel(
            new String[]{"Dublin", "Kildare", "Sligo"});
        models[2] = new DefaultComboBoxModel(
            new String[]{"Leeds", "Manchester", "Liverpool"});

        models2d[0] = new DefaultComboBoxModel(
            new String[]{"   "});
        models2d[1] = new DefaultComboBoxModel(
            new String[]{"DublinFC", "KildareFC", "SligoFC"});
        models2d[2] = new DefaultComboBoxModel(
            new String[]{"LeedsFC", "ManchesterFC", "LiverpoolFC"});



        combo2.setModel(models[1]);
        combo3.setModel(models2d[0]);
        this.add(combo1);
        this.add(combo2);
        this.add(combo3);
        combo1.addActionListener(this);
        combo2.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int k = combo1.getSelectedIndex();
        System.out.println(k);
        combo2.setModel(models[k]);


        int i = combo2.getSelectedIndex();

        combo3.setModel(models2d[i]);





//        int j = combo2.getSelectedIndex();
//        combo2.setModel(models[j]);
    }





    @Override
    public void run() {
        JFrame f = new JFrame("ComboTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new ComboTest());
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Dwayne Patel
  • 315
  • 2
  • 13
  • This has been asked many *many* times before. For example, please review the hits from [this StackOverflow search](http://stackoverflow.com/search?q=%5Bjava%5D+%5Bswing%5D+jcombobox+another+jcombobox). Consider searching the site in the future if you have similar questions that are likely to be very common. – Hovercraft Full Of Eels Apr 30 '15 at 16:23
  • Have done, i can find a solution for 2 linked combobox's bot not three. – Dwayne Patel Apr 30 '15 at 19:19
  • Extrapolate from the 2 combo solution to 3 combos. The principles are all the same. – Hovercraft Full Of Eels Apr 30 '15 at 20:28

0 Answers0