我在面板上使用组合框,据我所知,我们可以仅添加带有文本的项目
comboBox.addItem('item text');
但有时我需要使用项目和项目文本的某些值,例如在 html select 中:
<select><option value="item_value">Item Text</option></select>
有没有办法在组合框项目中同时设置值和标题?
现在我使用哈希来解决这个问题。
将值包装在类中并覆盖toString()
method.
class ComboItem
{
private String key;
private String value;
public ComboItem(String key, String value)
{
this.key = key;
this.value = value;
}
@Override
public String toString()
{
return key;
}
public String getKey()
{
return key;
}
public String getValue()
{
return value;
}
}
将 ComboItem 添加到您的组合框。
comboBox.addItem(new ComboItem("Visible String 1", "Value 1"));
comboBox.addItem(new ComboItem("Visible String 2", "Value 2"));
comboBox.addItem(new ComboItem("Visible String 3", "Value 3"));
每当您获得所选项目时。
Object item = comboBox.getSelectedItem();
String value = ((ComboItem)item).getValue();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)