Possible Duplicate:
assign keys for combo box in java
I am using JComboBox control in a Swing application. I have to add employee code and his name on JComboBox but i want to display only employee name not the code.
but when i select the employee name it should return the corresponding employee code.
what is the best and easy solution for it.
I am using the following code for adding the items on 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.
}
Instead of adding the code to the JComboBox, you have to add the Employee object (with the two members). Then you have to create a custom ListCellRenderer (extending DefaultListCellRenderer) to render the employee name.
Take a look at Oracle’s tutorial.