I do not use Hibernate, but given the JPA object named Customer and the JPA controller named CustomerJpaController you can do something like this.
Update: The code has been updated to reflect the transition to EclipseLink (JPA 2.1) as a save library.
import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JComboBox; import javax.swing.JFrame; public class CustomerTest implements Runnable { public static void main(String[] args) { EventQueue.invokeLater(new CustomerTest()); } @Override public void run() { CustomerJpaController con = new CustomerJpaController( Persistence.createEntityManagerFactory("CustomerPU")); List<Customer> list = con.findCustomerEntities(); JComboBox combo = new JComboBox(list.toArray()); combo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox) e.getSource(); Customer c = (Customer) cb.getSelectedItem(); System.out.println(c.getId() + " " + c.getName()); } }); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(combo); f.pack(); f.setVisible(true); } }
Objects added to the JComboBox get their display name from the toString() object method, so Customer been changed to return getName() to display:
@Override public String toString() { return getName(); }
You can learn more about JComboBox in the article How to use Combo Boxes .
source share