Java 引用动态添加的JComboBox值
我是Java新手(从-gulp-VisualBasic转换过来),到目前为止,我对本网站提供的所有示例都很满意。有一件事我搞不清楚,那就是如何引用动态添加的组合框和标签(或者为它们分配侦听器)。这是我的习题集: 我正在编写一个程序,从excel导入项目,并将它们输出到Google Earth的KML文件中。由于我不要求用户填充预先指定的Excel格式,因此我需要考虑Excel中不同字段位置的变化。我已经知道了如何通过迭代器添加组合框,但阻止我的是如何在以后使用它们引用标签(因为它们都是使用名称“comboBox”和“label”创建的) 我有一个Excel文件的示例,还有我的GUI,但不幸的是,我没有足够的代表发布图片。为了描述它,用户导入一个Excel文件(带有一个标题栏,说明列包含的内容:ID、描述、注释、位置),迭代器创建等量的组合框,让用户选择哪一列与Google Earth的三个设置选项(标签、描述、位置)匹配。我需要每个组合框做的是引用它对应的标签('label'='ID'),因此当我运行导出功能时,相应的单元格数据填充到KML文件中Java 引用动态添加的JComboBox值,java,dynamic,combobox,reference,Java,Dynamic,Combobox,Reference,我是Java新手(从-gulp-VisualBasic转换过来),到目前为止,我对本网站提供的所有示例都很满意。有一件事我搞不清楚,那就是如何引用动态添加的组合框和标签(或者为它们分配侦听器)。这是我的习题集: 我正在编写一个程序,从excel导入项目,并将它们输出到Google Earth的KML文件中。由于我不要求用户填充预先指定的Excel格式,因此我需要考虑Excel中不同字段位置的变化。我已经知道了如何通过迭代器添加组合框,但阻止我的是如何在以后使用它们引用标签(因为它们都是使用名称“
提前感谢您的帮助 无论何时动态添加对象,都可以将其添加到arraylist中。这将为您保留一个引用,您可以在需要时遍历arraylist。在阅读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)
}
}
}
}
//要包含筛选器的对象
公共类过滤器选举{
私人JLabel进口;
私人JComboBox导入选择;
公共过滤器选择(JLabel导入选项、JComboBox导入选项){
this.importOption=importOption;
this.importSelection=importSelection;
}
公共JComboBox getImportSelection(){
返回此.importSelection;
}
}
//构建一个过滤器,以确定Excel中的每列单元格所代表的内容
公共类ImportFilter扩展JDialog{
私有树映射头列表=新树映射();
private ArrayList filterList=新建ArrayList();
公共导入筛选器(…){
getContentPane().setLayout(新的BorderLayout());
buildFilterSelection(…);
addFilter(headerList);
}
公共无效生成器筛选器选择(…){
//从Excel单元格中的数据生成树映射
for(迭代器cit=row.cellIterator();cit.hasNext();){
headerList.put(cell.getColumnIndex(),cell.getStringCellValue().trim());
}
}
公共无效添加过滤器(树状图标题列表){
JPanel importFilter=新的JPanel();
JScrollPane scrollPane=新的JScrollPane(导入过滤器);
添加(滚动窗格,BorderLayout.CENTER);
//迭代树映射,为每个Excel单元格创建一个jComboBox和JLabel,并在标题行中包含数据
Set=headerList.entrySet();
迭代器头迭代器=set.Iterator();
while(headerierator.hasNext()){
Map.Entry headerMap=(Map.Entry)headerEditor.next();
字符串头=(字符串)headerMap.getValue();
最终JComboBox组合框;
JLabel标签=新的JLabel(标题);
comboBox=新的JComboBox(新字符串[]{“,”位置“,”纬度“,”经度“,”标签“,”说明“});
//使用JLabel文本确定要在JComboBox中选择的对象
开关(label.getText().toUpperCase()){
案例“地点”:
如果(locationFilter==false){
组合框。设置所选索引(1);
locationFilter=true;
}
打破
案例“名称”:
if(labelFilter==false){
组合框。设置所选索引(4);
labelFilter=true;
}
打破
}
//将JComboBox和JLable添加到ArrayList
过滤器选择过滤器=新过滤器选择(标签,组合框);
filterList.add(过滤器);
comboBox.addItemListener(新的ItemListener(){
公共无效itemStateChanged(ItemEvent ie){
//当JComboBox被更改时,在这里执行一些操作
}
});
//将JComboBox和JLabel添加到JPanel
导入过滤器。添加(标签);
importFilter.add(组合框);
}
}
}
//运行整个Excel工作表并引用筛选器,以确定单元格如何基于其列进行格式化
公共类PointsBuilder{
公共无效点生成器(ArrayList筛选器列表){
for(迭代器cit=row.cellIterator();cit.hasNext();){
Cell Cell=cit.next();
cell.setCellType(cell.cell\u TYPE\u字符串);
试一试{
//检查正在读取的单元格对应列的筛选器
开关(filterList.get(cell.getColumnIndex()).getImportSelection().getSelectedIndex()){
案例0:
//此单元格的列在筛选器中没有值
打破
案例1:
//此单元格的列包含一个位置
打破
案例4:
//此单元格的列包含一个标签
打破
案例5:
//此单元格的列包含说明