How do I create a java method to load a jcomboBox with JPA?

1

I am trying to make a method to load a bombobox in an application of using . I use:

  • NETBEANS 8.2
  • JAVA version "1.8.0_131"
  • EclipseLink (JPA 2.1)
  • JDBC Library

Form code

public jfrm_Administrador() {
    initComponents();
    emf = Persistence.createEntityManagerFactory("AdministradorRolesPU");
    em = emf.createEntityManager();
    loadComboBox();
}

And the method of loading jcombobox that does NOT work

public void loadComboBox() {
    //Creamos una Query
    //"Pais.findAll" es una query que fue definida automaticamente gracias al mapeo de la db
    //Luego obtenemos los resultados y los recorremos
    Iterator it = em.createNamedQuery("Tblpermiso.findByDescripcion").getResultList().iterator();
    while(it.hasNext()) {
        //Agrego el resultado al Combobox
        this.jComboBox1.addItem(((Tblpermiso)it.next()));
    }
}

How do I do the method correctly?

    
asked by Interes Ciencia 28.07.2017 в 19:56
source

1 answer

0

Your problem is in this line

      this.jComboBox1.addItem(((Tblpermiso)it.next()));

The program should not work since you can not cast a Tblpermiso to a String.

I do not know exactly what fields your table has but one way to correct it would be by accessing the attributes of the object that you are interested in, then concatenating them and now adding them to the combobox.

while (it.hasNext()) {

    Tblpermiso permiso = it.next();
    jComboBox1.addItem(permiso.getId() + " " + permiso.getNombre());

}
    
answered by 28.07.2017 / 20:56
source