数组 JComboBox 列表



>我创建了一个 JComboBox 的 ArrayList

private static ArrayList<JComboBox> comboList = new ArrayList<>();

然后将每个JComboBox实例添加到ArrayList

private void courseUnit() {
        String[] units = {"6", "5", "4", "3", "2", "1"};
        int x = 520, y = 30;
        for (int i = 0; i < 10; i++) {
            comboUnits = new JComboBox<>(units);
            comboUnits.setBounds(x, y, 100, 25);
            y += 30;
            comboUnits.setToolTipText("Select course unit");
            comboUnits.setVisible(true);
            comboUnits.addActionListener(new PaneAction());
            add(comboUnits);
            comboList.add(comboUnits); //comboUnits added to ArrayList
        }
    }

我的问题是,我如何从 ArrayList 中的每个组合框中获取选定的项,因为我尝试过这个

//this is supposed to get the selected item of the first ComboBox and assign to courseGrade[0]
    public void actionPerformed(ActionEvent evt) {
            String[] courseGrade = new String[10];
            courseGrade[0] = (String)comboList.get(0).getSelectedItem();

并且程序没有编译。

您可以在将

JComboBox添加到ArrayList时附加ActionListner

    private void courseUnit() {
            String[] units = {"6", "5", "4", "3", "2", "1"};
            int x = 520, y = 30;
            for (int i = 0; i < 10; i++) {
                comboUnits = new JComboBox<>(units);
                comboUnits.setBounds(x, y, 100, 25);
                y += 30;
                comboUnits.setToolTipText("Select course unit");
                comboUnits.setVisible(true);
                //comboUnits.addActionListener(new PaneAction());
                add(comboUnits);
//////////////// Here are the changes
                comboUnits.addActionListener (new ActionListener () {
                   public void actionPerformed(ActionEvent e) {
                      String selectedItem = (String)e.getSelectedItem();
                   }
                });
                comboList.add(comboUnits); //comboUnits added to ArrayList
            }
        }

我终于明白了。我必须将 ArrayList 创建为静态实例变量

private static ArrayList<JComboBox> comboList;

然后在构造函数中实例化它

comboList = new ArrayList<>();

新的 courseUnit() 方法现在看起来像这样

private void courseUnit() {
        String[] units = {"6", "5", "4", "3", "2", "1"};
        int x = 520, y = 30;
        for (int i = 0; i < 10; i++) {
            comboUnits = new JComboBox<>(units);
            comboUnits.setBounds(x, y, 100, 25);
            comboUnits.setToolTipText("Select course unit");
            y += 30;
            comboUnits.setVisible(true);
            add(comboUnits);
            //I added ActionListener to each comboBox in the ArrayList,
            //thanks to Junaid for this
            comboUnits.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){    
                    ArrayList<String> courseGrade = new ArrayList<>();
                    JComboBox comboBox = (JComboBox) evt.getSource();
                    courseGrade.add((String) comboBox.getSelectedItem());
                }
            });
            comboList.add(comboUnits);
      }//end of loop
    } //end of method

最新更新