JComboBoxes的ArrayList

Orobo幸运的奥索卡

我创建了一个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中的每个comboBox中获取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();

而且程序没有编译。

朱尼德

ActionListner新增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);

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章