Heyy

I'm creating a small swing based application with hibernate in java. And That I want fill combobox from database coloumn. The Way I can perform that?

And I'm not sure in where(under initComponents, buttonActionPerformd) I have to do.

To save I'am using jbutton and it is code is here now :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

 int idd=Integer.parseInt(jTextField1.getText());

 String name=jTextField2.getText();

 String description=jTextField3.getText();

 Session session = null;

 SessionFactory sessionFactory = new Configuration().configure()
    .buildSessionFactory();

 session = sessionFactory.openSession();

 Transaction transaction = session.getTransaction();

   try {


       ContactGroup con = new ContactGroup();

       con.setId(idd);

       con.setGroupName(name);
       con.setGroupDescription(description);



       transaction.begin(); 
       session.save(con); 
       transaction.commit(); 


      } catch (Exception e) {
       e.printStackTrace();
      }

      finally{
       session.close(); 
      }    
}

I do not use Hibernate, but given a JPA entity named Customer along with a JPA controller named CustomerJpaController, that you can do something similar to this:

public class CustomerTest {

    public static void main(String[] args) {
        CustomerJpaController con = new CustomerJpaController();
        List<Customer> list = new ArrayList<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 put into a [cde] obtain display title in the object's JComboBox method, so toString() was modified to come back Customer for display reasons:

getName()

Discover more about [cde] within the article How to Use Combo Boxes.