Please can anyone help me with a simple sample on how to populate JCombobox with data(mysql etc) using Java persistence (JPA). I am quite new to this component so I desperately need help with this.

Dani AI

Generated

The original question from asked for a simple sample; following 's hint that items must be added to the combo, here is a practical, JPA-friendly pattern that avoids blocking the UI thread, keeps display logic separate, and lets the combo hold entity objects (so selections map back to IDs).

Use a background task (SwingWorker) to run the JPA query, then build a DefaultComboBoxModel on the EDT:

// assume 'emf' is an application-wide EntityManagerFactory
private void loadCombo() {
    SwingWorker<List<MyEntity>,Void> worker = new SwingWorker<List<MyEntity>,Void>() {
        @Override
        protected List<MyEntity> doInBackground() {
            EntityManager em = emf.createEntityManager();
            try {
                return em.createQuery("SELECT e FROM MyEntity e ORDER BY e.name", MyEntity.class)
                         .getResultList();
            } finally {
                em.close();
            }
        }
        @Override
        protected void done() {
            try {
                List<MyEntity> list = get();
                DefaultComboBoxModel<MyEntity> model = new DefaultComboBoxModel<>();
                for (MyEntity e : list) model.addElement(e);
                comboBox.setModel(model);
            } catch (Exception ex) {
                // log/handle
            }
        }
    };
    worker.execute();
}

Keep display and identity clean: either implement toString() on the entity to return the display name, or set a renderer so the combo shows a friendly label while the selected item remains the entity:

comboBox.setRenderer(new DefaultListCellRenderer() {
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        setText(value == null ? "" : ((MyEntity) value).getName());
        return this;
    }
});

Notes and cautions: reuse EntityManagerFactory (don’t recreate it for each load), close EntityManagers, and avoid accessing lazy relationships after the EM is closed (use DTOs or eager fetches for fields needed in the UI). To get the selected id: MyEntity sel = (MyEntity) comboBox.getSelectedItem(); Long id = (sel == null) ? null : sel.getId();. This pattern keeps the UI responsive and maps selections back to JPA entities safely.

Recommended Answers

All 3 Replies

use addItem() method of JCombobox class for adding data to your combobox which is placed on your GUI.i d'not know exactly how to use JPA in java but the following code will help you see it

if you know how to read data using and for example (if your data is in result set object)

jComboBox1.removeAllItems();
/* update fresh data to JcomboBox1 component as follows*/
while(rs.next){
    jCombobox1.addItem(rs.getString(1));
}

all the best,
radha krishna.p

use addItem() method of JCombobox class for adding data to your combobox which is placed on your GUI.i d'not know exactly how to use JPA in java but the following code will help you see it

if you know how to read data using and for example (if your data is in result set object)

jComboBox1.removeAllItems();
/* update fresh data to JcomboBox1 component as follows*/
while(rs.next){
    jCombobox1.addItem(rs.getString(1));
}

all the best,
radha krishna.p

use addItem() method of JCombobox class for adding data to your combobox which is placed on your GUI.i d'not know exactly how to use JPA in java but the following code will help you see it

if you know how to read data using and for example (if your data is in result set object)

jComboBox1.removeAllItems();
/* update fresh data to JcomboBox1 component as follows*/
while(rs.next){
    jCombobox1.addItem(rs.getString(1));
}

all the best,
radha krishna.p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.