This discussion is archived
4 Replies Latest reply: Nov 22, 2012 6:32 AM by KwesiAryee RSS

How can i populate the Jcombobox with the data from my jpa list? Desperate

KwesiAryee Newbie
Currently Being Moderated
I need to populate my jcombobox with data from my jpa. This is my first time trying this and i just need some sample or a quick solution to this. Thanks in advance

<code>
public static MyNames myNames() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaTrialsPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Query q = em.createNamedQuery("MyNames.findAll");
MyNames n = (MyNames) q.getResultList();
return n;
}

public static void myCombo() {       
comboBox = new JComboBox(????--how can i make the jcombobox display my myNames() list--?????);
frame.add(comboBox, BorderLayout.PAGE_START);
}
</code>
  • 1. Re: How can i populate the Jcombobox with the data from my jpa list? Desperate
    gimbal2 Guru
    Currently Being Moderated
    Implement toString() on your entity to return whatever you want to use as a label for the combobox.

    Your need for quick solutions is very destructive to your learning process. In stead take the time and read around a little. Swing happens to have an excellent tutorial with many example programs readily available for you to pick through:

    http://docs.oracle.com/javase/tutorial/uiswing/


    And PS: you need to use \
     tags in this forum.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 2. Re: How can i populate the Jcombobox with the data from my jpa list? Desperate
    KwesiAryee Newbie
    Currently Being Moderated
    I have successfully created the jcombobox with the data from jpa. my primary issue is with how it displays like the following;

    *entities.MyNames[nameId=1]*

    yet I don't want to change my entities class toString. Is there anyway I can get just the lists as string array? My revised code is as follows;
    public static void myCombo() {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaTrialsPU");
            EntityManager em = emf.createEntityManager();
            em.getTransaction().begin();
            try {
                Query q = em.createNamedQuery("MyNames.findAll");
                List<MyNames> name = new ArrayList<>(q.getResultList());
    
                comboBox = new JComboBox(name.toArray());
    //            comboBox = new JComboBox(myData());
                frame.add(comboBox, BorderLayout.PAGE_START);
            } catch (Exception e) {
                e.getMessage();
                em.getTransaction().rollback();
            } finally {
                em.close();
            }
    
        }
  • 3. Re: How can i populate the Jcombobox with the data from my jpa list? Desperate
    gimbal2 Guru
    Currently Being Moderated
    Kwesi Aryee wrote:
    yet I don't want to change my entities class toString.
    Up to you, but you asked for a quick fix - now you reject one. What's it going to be dude?

    Other than that: why don't you go check out the section on the combobox in the link I gave you? I can't speak for you, but I tend to find the answer to a Swing question in a manner of minutes using it.
  • 4. Re: How can i populate the Jcombobox with the data from my jpa list? Desperate
    KwesiAryee Newbie
    Currently Being Moderated
    Thanks a lot for your time, I have finally been able to achieve my object. As you said there is no quick fix(especially in programming). Thanks a lot.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points