一个jcombobox填充另一个jcombobox来填充第三个jcombobox

时间:2015-04-30 16:15:55

标签: java arrays swing jcombobox

需要帮助A jcombobox填充另一个jcombobox来填充第三个jcombobox。

我可以使用我发现的代码来管理两个代码,并且一直在使用2d数组,但似乎无法连接三个jcombobox。

这是迄今为止的代码:

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

0 个答案:

没有答案