Это можно сделать так:
import javax.swing.*;
import java.awt.FlowLayout;
import java.util.*;
public class JComboBoxExample {
public static void main(String[] args) {
List<Employee> employeesFromDatabase = Arrays.asList(
new Employee(101, "John"),
new Employee(102, "Kevin"),
new Employee(103, "Sally"),
new Employee(104, "Sara"));
JComboBox<String> comboBox = new JComboBox<>();
for (Employee employee : employeesFromDatabase) {
StringJoiner stringJoiner = new StringJoiner(", ");
stringJoiner.add(Integer.toString(employee.getId()));
stringJoiner.add(employee.getName());
comboBox.addItem(stringJoiner.toString());
}
JFrame f = new JFrame("Questions");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(new JLabel("Employee"));
f.getContentPane().add(comboBox);
f.setBounds(300, 200, 400, 300);
f.setVisible(true);
}
}
class Employee {
private int id;
private String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}