So what you actually need to do, is to simply overwrite the toString() method to just return the name. I made a simple example to show it:
main method:
public static void main(String[] args) {
JFrame f = new JFrame();
List<Client> cs = new ArrayList();
cs.add(new Client("Hans", 1));
cs.add(new Client("Peter", 2));
JComboBox c = new JComboBox<Client>(cs.toArray(new Client[] {}));
c.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println(((Client) e.getItem()).getAge());
}
});
f.add(c);
f.pack();
f.setVisible(true);
}
And the Client class:
public class Client {
String name;
int age;
public Client(String n, int age) {
name = n;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return getName();
}
}
This works perfectly find. Each entry in the JComboBox only shows the name
toStringmethod should do the trickobjectList.stream().map(s -> s.getName()).collect(Collectors.toList())