引用动态添加的 JComboBox 值

Referencing JComboBox values that are added dynamically

我是 Java 的新手(已从 -gulp- VisualBasic 切换过来),到目前为止,我对本网站提供的所有示例都非常满意。我无法弄清楚的一件事是如何引用动态添加的组合框和标签(或为它们分配监听器)。这是我的问题集:

我正在编写一个程序来从 excel 导入项目并将它们输出到 Google 地球的 KML 文件中。由于我不要求用户填充 pre-specified Excel 格式,因此我需要考虑 Excel 中不同字段位置的变化。我已经想出如何通过迭代器添加组合框,但让我停下来的是如何在以后使用它们来引用标签(因为它们都是用名称 'comboBox' 和 'label' 创建的) .

我有一个 Excel 文件的示例,以及我的 GUI,但不幸的是,我没有足够的代表来 post 图片。为了描述它,用户导入一个 Excel 文件(标题栏说明该列包含的内容:ID、DESCRIPTION、COMMENTS、LOCATION),然后迭代器创建相同数量的组合框供用户选择他们的哪一列与 Google 地球的三个设置选项(标签、描述、位置)相匹配。我需要每个组合框做的是引用它对应的标签 ('Label' = 'ID') 所以当我 运行 导出功能时,相应的单元格数据会填充到 KML 文件中。

在此先感谢您的帮助!

任何时候动态添加对象时,也将其添加到数组列表中。这将为您保留一个引用,您可以在需要时遍历数组列表。

阅读 Scott Corbett 的回复后,我想出了解决这个问题的方法,现在正着手发布答案。我删除了很多参数和变量,因为它们无关紧要,同时仍在尝试提供一个对我有用的示例。

//object to contain the filter
public class FilterSelection {
    private JLabel importOption;
    private JComboBox importSelection;

    public FilterSelection (JLabel importOption, JComboBox importSelection){
        this.importOption = importOption;
        this.importSelection = importSelection;
    }

    public JComboBox getImportSelection() {
        return this.importSelection;
    }
}

//builds a filter to determine what each column in Excel the cells represent
public class ImportFilter extends JDialog {
    private TreeMap<Integer, String> headerList = new TreeMap<Integer, String>();
    private ArrayList<FilterSelection> filterList = new ArrayList<FilterSelection>();

    public ImportFilter(...) {
        getContentPane().setLayout(new BorderLayout());
        buildFilterSelection(...));
        addFilter(headerList);
    }

    public void buildFilterSelection(...) {
        //builds a TreeMap from data in Excel cells
        for(Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
            headerList.put(cell.getColumnIndex(), cell.getStringCellValue().trim());
        }
    }

    public void addFilter(TreeMap<Integer, String> headerList) {
        JPanel importFilter = new JPanel();
        JScrollPane scrollPane = new JScrollPane(importFilter);
        contentPane.add(scrollPane, BorderLayout.CENTER);

        //iterates through the TreeMap to create a JComboBoxe and JLabel for each Excel cell with data in the header row
        Set set = headerList.entrySet();
        Iterator headerIterator = set.iterator();
        while (headerIterator.hasNext()) {
            Map.Entry headerMap = (Map.Entry)headerIterator.next();
            String header = (String) headerMap.getValue();

            final JComboBox comboBox;
            JLabel label = new JLabel(header);
            comboBox = new JComboBox(new String[] {"", "Location", "Latitude", "Longitude", "Label", "Description"});

            //uses JLabel text to determine what object to select in the JComboBox
            switch (label.getText().toUpperCase()) {
            case "LOCATION":
                if (locationFilter == false) {
                    comboBox.setSelectedIndex(1);
                    locationFilter = true;
                }
                break;
            case "NAME":
                if (labelFilter == false) {
                    comboBox.setSelectedIndex(4);
                    labelFilter = true;
                    }
                break;
            }

            //adds JComboBox and JLable to ArrayList
            FilterSelection filter = new FilterSelection(label, comboBox);
            filterList.add(filter);
            comboBox.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent ie) {
                    //do something here when the JComboBox is changed
                }
            });

            //adds JComboBox and JLabel to the JPanel
            importFilter.add(label);
            importFilter.add(comboBox);
        }
    }
}

//runs through an entire Excel sheet and references the filter to determine how the cells are formatted based on their column
public class PointsBuilder {
    public void PointsBuilder (ArrayList<FilterList> filterList) {
        for(Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
            Cell cell = cit.next();
            cell.setCellType(Cell.CELL_TYPE_STRING);
            try {
                //checks the filter for the corresponding column of the cell being read
                switch (filterList.get(cell.getColumnIndex()).getImportSelection().getSelectedIndex()) {
                    case 0:
                        //the column of this cell had no values in the filter
                        break;
                    case 1:
                        //the column of this cell contains a location
                        break;
                    case 4:
                        //the column of this cell contains a label
                        break;
                    case 5:
                        //the column of this cell contains a description
                        break;
                }
            } catch (IndexOutOfBoundsException e) {
                //this block catches any data that is in columns beyond where the filter ended based on the header info (ex: last header column = 5; current cell column = 7)
            }
        }
    }
}