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