我想通过值而不是索引在JComboBox中设置所选索引.怎么做?例
public class ComboItem {
private String value;
private String label;
public ComboItem(String value, String label) {
this.value = value;
this.label = label;
}
public String getValue() {
return this.value;
}
public String getLabel() {
return this.label;
}
@Override
public String toString() {
return label;
}
}
JComboBox test = new JComboBox();
test.addItem(new ComboItem(0, "orange"));
test.addItem(new ComboItem(1, "pear"));
test.addItem(new ComboItem(2, "apple"));
test.addItem(new ComboItem(3, "banana"));
test.setSelectedItem("banana");
Run Code Online (Sandbox Code Playgroud)
好的,我已经修改了一下我的问题.我忘记了我的JComboBox里面有一个自定义项目,这让它变得有点困难.我不能做setSelectedItem,因为我在每个项目中都有一个ComboItem.那么,我怎么做到这一点?