是否可以为a设置一个值和一个标签JComboBox
所以我可以显示标签但获得不同的值?
例如在 JavaScript 中我可以这样做:
document.getElementById("myselect").options[0].value //accesses value attribute of 1st option
document.getElementById("myselect").options[0].text //accesses text of 1st option
您可以将任何对象放入 JComboBox 中。默认情况下,它使用对象的 toString 方法来显示标签,并使用键盘在组合框中导航。因此,最好的方法可能是在组合内定义和使用适当的对象:
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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)