可能重复:
为java中的组合框分配键
我JComboBox
在 Swing 应用程序中使用控件。我必须添加员工代码和他的姓名,JComboBox
但我只想显示员工姓名而不是代码。
但是当我选择员工姓名时,它应该返回相应的员工代码。什么是最好和最简单的解决方案。我正在使用以下代码添加项目JComboBox
try
{
JComboBox jc1= new JComboBox();
jc1.addItem("X");
jc1.addItem("Y");
jc1.addItem("Z");
}
public void itemStateChanged(ItemEvent ie)
{
String code=(String)jc1.getSelectedItem();
//while items being selected it should return the emp code of the given emp name
//eg if user selects X it should return the emp code corresponding to X.
}