Java jComboxes的ArrayList

Java jComboxes的ArrayList,java,swing,jcombobox,Java,Swing,Jcombobox,我创建了JComboBox的ArrayList private static ArrayList<JComboBox> comboList = new ArrayList<>(); 我的问题是,如何从ArrayList中的每个组合框中获取selectedItem,因为我尝试了这个方法 //this is supposed to get the selected item of the first ComboBox and assign to courseGrade[0]

我创建了JComboBox的ArrayList

private static ArrayList<JComboBox> comboList = new ArrayList<>();
我的问题是,如何从ArrayList中的每个组合框中获取selectedItem,因为我尝试了这个方法

//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


我终于明白了。我必须将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

发布编译错误java.lang.ExceptionInInitializerError,原因是:java.lang.NullPointerException这不是编译错误,而是代码引发的异常。您应该检查堆栈跟踪,它应该告诉您发生NullPointerException的位置。你忘了在那一行初始化一些东西。我已经跟踪到了,但它仍然抛出一个异常。ActionEvent中不存在静态方法e.getItem,但我想你指的是e.getSelectedItem;答案已更新。如果你觉得我的答案有帮助,那么请把它标记为正确。谢谢。这很有帮助
comboList = new 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);
            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