【发布时间】:2018-07-02 16:26:09
【问题描述】:
在 Java 中,我试图确定刚刚单击了 ArrayList 中的哪个 JComboBox。部分代码如下:
private ArrayList<JComboBox<String>> setTextBoxList;
// basic initialization
public void populateList() {
String str[] = {"one", "two"};
for(int i=0; i<2; i++) {
JComboBox<String> jcb = new JComboBox<String>(str);
setTextBoxList.add(new JComboBox<String>(str));
jcb.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o instanceof JComboBox) {
// here's where I'd like to see which box was just changed
System.out.println("change index "
+ setTextBoxList.indexOf((JComboBox)o) );
}
}
我的问题是,当我单击并更改其中一个组合框时,显示的索引始终为 -1。我想获取刚刚单击/更改了arraylist中哪个框的索引。没有显式类型转换,我得到相同的结果。
【问题讨论】: