引用动态添加的JComboBox值

时间:2015-04-28 21:37:46

标签: java dynamic combobox reference

我是Java的新手(从-gulp-VisualBasic切换过来),到目前为止,我已经与本网站提供的所有示例相处得很好。我无法弄清楚的一件事是如何引用动态添加的组合框和标签(或将侦听器分配给它们)。这是我的问题集:

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

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

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

无论何时动态添加对象,也可以将其添加到arraylist中。这将为您保留一个参考,您可以在需要找到它时遍历arraylist。

答案 1 :(得分:0)

在阅读了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)
            }
        }
    }
}