If you have a collection of objects and want to display a value, you can use the Vector class as the model and then create a custom renderer using the BasicComboBoxRenderer class to display the text.
Vector<Employee> data = new Vector<Employee>();
data.addElement(new Employee(1001, "John Smith"));
data.addElement(new Employee(1002, "Linda Baker"));
data.addElement(new Employee(1003, "Youcef Hussain"));
data.addElement(new Employee(1004, "Jia Lia"));
JComboBox jcb = new JComboBox(data);
jcb.setRenderer(new ComboBoxRenderer());
class ComboBoxRenderer extends javax.swing.plaf.basic.BasicComboBoxRenderer
{
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus)
{
Employee emp = (Employee)value;
setText(emp.getName());
return this;
}
}
class Employee{
protected int employee_id;
public String employee_name;
public Employee(int id, String name){
this.employee_id = id;
this.employee_name = name;
}
public String getName(){
return this.employee_name;
}
}
Full code sample can be found here http://www.hostprojects.net/snippets/java/158/basiccomboboxrenderer-example