Problem with data loaded into the table

Hi, I’m new in Vaadin and I’m doing a form for data processing.
I could not move much with jpa by the characteristics of the network (like proxy and others) so I had to do many things by hand.
I have a problem on a form, I could load the data, I works fine, I fill in the fields, but dont know how to process the form.
If it is a type ComboBox or NativeSelect fielt etc, I can capture fine but I can not capture the data loaded into the table.
step classes to see if I can help.
I hope you understand and can help me, I accept any suggestion
In the original form call a class that fills me tables

//---------------------------------
try {
new RellenarTablasIntegrantes(integrantesTable, AgregarIntegrantes);
} catch (Exception e) {

System.out.println(“no funciona la instanciación del RellenarCombos”);
}
//---------------------------------

This is the class that populates the tables
Instance others to do querys by hand.

public class RellenarTablasIntegrantes{

String LEGAJO = "Legajos";
String NOMBRE = "Nombre";
String CATEGORIA = "Categoría";
String ESCALAFON = "Escalafon";
String RESPONSABLE = "Responsable";
String BORRAR = "Borrar";
String NUMERO = "N°";
private int cont = 0;

public RellenarTablasIntegrantes(final Table integrantesTable, Button AgregarIntegrantes) {
    integrantesTable.addContainerProperty(LEGAJO, ComboBox.class, null);
    integrantesTable.setColumnWidth(LEGAJO, 100);
    integrantesTable.addContainerProperty(NOMBRE, ComboBox.class, null);
    integrantesTable.setColumnWidth(NOMBRE, 270);
    integrantesTable.addContainerProperty(CATEGORIA, ComboBox.class, null);
    integrantesTable.setColumnWidth(CATEGORIA, 150);
    integrantesTable.addContainerProperty(ESCALAFON, ComboBox.class, null);
    integrantesTable.setColumnWidth(ESCALAFON, 150);
    integrantesTable.addContainerProperty(RESPONSABLE, CheckBox.class, null);
    integrantesTable.setColumnWidth(RESPONSABLE, 130);
    integrantesTable.addContainerProperty(BORRAR, Button.class, null);
    integrantesTable.setColumnWidth(BORRAR, 50);
    integrantesTable.addContainerProperty(NUMERO, Label.class, null);
    integrantesTable.setColumnWidth(NUMERO, 50);
    
    // Listener del boton agregar integrantes
    AgregarIntegrantes.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            cont++;
            setintegrantetable(cont, integrantesTable);
        }
    });        
}

@SuppressWarnings("unchecked")
private void setintegrantetable(int cont, final Table integrantesTable) {
    // integrantesTable.setImmediate(true);
    try {
        final Object itemId = integrantesTable.addItem();

        // Insert the data and the additional component column
        for (int i = 0; i < cont; i++) {
        
            // cargo los tipos de dato
            ComboBox legajo = new ComboBox(null);
            legajo.setWidth("95px");
            ComboBox nombre = new ComboBox(null);
            nombre.setWidth("265px");
            ComboBox categoria = new ComboBox(null);
            categoria.setWidth("145px");
            final ComboBox escalafon = new ComboBox(null);
            escalafon.setWidth("145px");
            CheckBox responsable = new CheckBox(null);
            
            //Instancio     el boton y le seteo el titulo
            Button borrar = new Button("Borrar");
            
            //creo un listener de boton para borrar esa linea segun el id seteado en el idemId
            borrar.addClickListener(new Button.ClickListener() {
                private static final long serialVersionUID = 1L;
                public void buttonClick(ClickEvent event) {
                    integrantesTable.removeItem(itemId);
                }
            });                
            
            // seteComboBoxo el numero del trabajo
            String num = Integer.toString(cont);
            Label numA = new Label(num);

            // Relleno los Comboboxes con lo queComboBox traigo de la base.
            FillcomboLegajo(legajo, nombre, categoria, escalafon);

            //---------------- Encadenando Combos --------------------------
            
            RellenarCombosEventos relleN = new RellenarCombosEventos();
             relleN.RellComEvNom(legajo, nombre, itemId);
             relleN.RellComEvLeg(legajo, nombre, itemId);

            //-------------------------------------------
            
            integrantesTable.getItem(itemId).getItemProperty(LEGAJO).setValue(legajo);
            integrantesTable.getItem(itemId).getItemProperty(NOMBRE).setValue(nombre);
            integrantesTable.getItem(itemId).getItemProperty(CATEGORIA).setValue(categoria);
            integrantesTable.getItem(itemId).getItemProperty(ESCALAFON).setValue(escalafon);
            integrantesTable.getItem(itemId).getItemProperty(RESPONSABLE).setValue(responsable);
            integrantesTable.getItem(itemId).getItemProperty(BORRAR).setValue(borrar);
            integrantesTable.getItem(itemId).getItemProperty(NUMERO).setValue(numA);
        }
    } catch (Exception e) {
        System.out
                .println("no funciona el rellenar tablas En rellenartablasIntegrantes");
    }

    try {
        integrantesTable.setPageLength(integrantesTable.size());
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

// legajo, nombre, categoria, escalafon
public void FillcomboLegajo(ComboBox area, ComboBox area1, ComboBox area2, ComboBox area3) {

    SelectEmpleados emple = new SelectEmpleados();
    
    try{
        for (String nom1:SelectEmpleados.getLegajo()){
            area.addItem(nom1.toString());
            area.setValue(area.getItemIds().iterator().next());
        }
    }catch(Exception e){
        System.out.println("error en legajo");
    }
    
    try{
        for (String nom1:SelectEmpleados.getApellidoNombres()){
            area1.addItem(nom1.toString());
            area1.setValue(area1.getItemIds().iterator().next());
        }
    }catch(Exception e){
        System.out.println("error en getApellidoNombres");
    }
    
    try{
        for (String nom2:SelectEmpleados.getCategoria()){
            area2.addItem(nom2.toString());
            area2.setValue(area2.getItemIds().iterator().next());
        }

    }catch(Exception e){
        System.out.println("error en getCategoria");
    }
    
    try{
        for (String nom3:SelectEmpleados.getCodEscalafon()){
            area3.addItem(nom3.toString());
            // con esto queda seleccionado el primer valor y no queda el
            // combo en blanco
            area3.setValue(area3.getItemIds().iterator().next());
        }
    }catch(Exception e){
        System.out.println("error en getEscalafon");
    }
}

}

Hi!

Could you explain your problem a little further? If I understand your situation correctly, you have a table which you can fill with rows of following type: {ComboBox, ComboBox, ComboBox, ComboBox, CheckBox, Button, Label}. You can get values from a row like this:

                        Object legajoValue = legajo.getValue();
                        Object nombreValue = nombre.getValue();
                        Object categoriaValue = categoria.getValue();
                        Object escalafonValue = escalafon.getValue();
                        boolean responsableValue = responsable.getValue();

If your problem was something different, please tell me.

First Sara thanks for your kindness.
Maybe my problem is that I have two separate classes (or that I’m very new to java), one that creates the tables and another that populates the table.
This table creates rows with components in fields.
What I need is to capture the data contained in each field in each row.
For example:
in the first row the name X and the category Y.
in the second row and the name J and the category K
third row and the name H and the category M
and so
And capture the data for load the tables in a database

Hi!

If you have several rows in your table, you need first to specify the specific row by:

integrantesTable.getItem(itemId) …where itemId is the line number

This you could get by:

String legajo1 = integrantesTable.getItem(1).getItemProperty(LEGAJO).getValue();
String categoria1 = integrantesTable.getItem(1).getItemProperty(CATEGORIA).getValue();
String legajo2 = integrantesTable.getItem(2).getItemProperty(LEGAJO).getValue();
String categoria2 = integrantesTable.getItem(2).getItemProperty(CATEGORIA).getValue();
String legajo3 = integrantesTable.getItem(3).getItemProperty(LEGAJO).getValue();
String categoria3 = integrantesTable.getItem(3).getItemProperty(CATEGORIA).getValue();

If you’re uncertain about the itemIds, perhaps you could store them in a list?

I hope this helped, otherwise you can reply with further questions :slight_smile:

Thanks Sara, I changed my class to use beans but I still can capture the data and now I can not erase the lines of the table
this is the new code with beans
/**

  • @author Esteban J Pagano
    */
    @SuppressWarnings(“serial”)
    public class RellenarTablasIntegrantes{

    private Object itemId;
    private int cont = 0;

    public RellenarTablasIntegrantes(){
    }
    //Create a bean container related to RH_Empleado
    final BeanItemContainer<RH_Empleado> beans = new BeanItemContainer<RH_Empleado>(RH_Empleado.class);

    public RellenarTablasIntegrantes(final Table integrantesTable, Button AgregarIntegrantes) {

//------------------------------------------------------------------------------------------------

    // Listener for add lines to de table
    AgregarIntegrantes.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            cont++;
            setintegrantetable(integrantesTable, cont);
        }
    });

//------------------------------------------------------------------------------------------------

}

private void setintegrantetable(final Table integrantesTable, int cont) {
    

    try {
        
        // Set the data type and With properties
        final ComboBox legajo = new ComboBox(null);
        legajo.setWidth("95px");
        ComboBox nombre = new ComboBox(null);
        nombre.setWidth("265px");
        ComboBox categoria = new ComboBox(null);
        categoria.setWidth("145px");
        final ComboBox escalafon = new ComboBox(null);
        escalafon.setWidth("145px");
        CheckBox responsable = new CheckBox(null);

            
            //Create a burron whit Borrar Captcha
            Button borrar = new Button("Borrar");
                            
            //Set a number for the table and cast to string for asign to a Label component
            Label numA = new Label(Integer.toString(cont));

            // Get the comboboxes data for the DB.
            FillcomboLegajo(legajo, nombre, categoria, escalafon);

            //---------------- relate 2 combos with listenes --------------------------
            
            RellenarCombosEventos relleN = new RellenarCombosEventos();
             relleN.RellComEvNom(legajo, nombre);
             relleN.RellComEvLeg(legajo, nombre);
         
          //add beans to de beancontainer
          Object itemId = beans.addBean(new RH_Empleado(legajo, nombre, categoria, escalafon, responsable, borrar, numA));
          //set the id
          setItemId(itemId);
          // Bind the table to the beancontainer
          integrantesTable.setContainerDataSource(beans);
          
          //try to get the value for the combobox
         Object legajoValue = integrantesTable.getItem(1).getItemProperty(nombre).getValue();
         System.out.println(legajoValue);
          
          
       //create a button listener of a button to delete that line according to the id in the idemId
            borrar.addClickListener(new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    //try to remove the line but dosen't work
                    integrantesTable.removeItem(getItemId());
                    //integrantesTable.removeAllItems(); //this work but remove all the table
                    integrantesTable.commit();    
                    System.out.println("boton borrar"+ getItemId());
                }
            });    
    } catch (Exception e) {
        System.out.println("error En rellenartablasIntegrantes");
    }
}


public void FillcomboLegajo(ComboBox area, ComboBox area1, ComboBox area2, ComboBox area3) {
                
 new SelectEmpleados();
    
    try{
        for (String nom1:SelectEmpleados.getLegajo()){
            area.addItem(nom1.toString());                
            // set the firs value
            area.setValue(area.getItemIds().iterator().next());
        }
    }catch(Exception e){
        System.out.println("error en legajo");
    }
    
    try{
        for (String nom1:SelectEmpleados.getApellidoNombres()){
            area1.addItem(nom1.toString());
            // set the firs value
            area1.setValue(area1.getItemIds().iterator().next());
        }
    }catch(Exception e){
        System.out.println("error en getApellidoNombres");
    }
    
    try{
        for (String nom2:SelectEmpleados.getCategoria()){
            area2.addItem(nom2.toString());
            // set the firs value
            area2.setValue(area2.getItemIds().iterator().next());
        }

    }catch(Exception e){
        System.out.println("error en getCategoria");
    }
    
    try{
        for (String nom3:SelectEmpleados.getCodEscalafon()){
            area3.addItem(nom3.toString());
            // set the firs value
            area3.setValue(area3.getItemIds().iterator().next());
        }
    }catch(Exception e){
        System.out.println("error en getEscalafon");
    }
}

public Object getItemId() {
    return itemId;
}

public void setItemId(Object itemId) {
    this.itemId = itemId;
}

public int getCont() {
    return cont;
}

public void setCont(int cont) {
    this.cont = cont;
} 

}

I change the delete button this and it works, now I just need to capture the data in the bean

integrantesTable.addGeneratedColumn(“Borrar”,
new Table.ColumnGenerator() {
public Object generateCell(

                       Table source,final Object itemId,Object columnId){
                         Button removeButton = new Button("X");
                         removeButton.addClickListener(new ClickListener(){
                           public void buttonClick(ClickEvent event) {
                              integrantesTable.removeItem(itemId);
                          }
                       });
                       return removeButton;
                     }
                   });
               
                   return integrantesTable;

Hi!

You need first to get the correct row in the table:

BeanItem<RH_Empleado> bie = beans.getItem(itemid); After that you can get the correct value for the cell by:

bie.getItemProperty(itemproperty);

The itemproperties are defined in your class RH_Empleado by their getters and setters.

Thanks again Sara.
I have still the same problem, and when I try to iterate over the BeanItemContainer this bean does not return any data

this is my code.

// Create a bean container related to RH_Empleado
final BeanItemContainer<RH_Empleado> beans = new BeanItemContainer<RH_Empleado>(RH_Empleado.class);

public RellenarTablasIntegrantes(final Table integrantesTable,    Button AgregarIntegrantes) {
    integrantesTables = integrantesTable;
    // Listener for add lines to de table
    AgregarIntegrantes.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            cont++;
            setintegrantetable(integrantesTable, cont);

//this is my modified routine to try Collect the results of the iteration into this string.
String items = “”;

        // Iterate over the item identifiers of the table.
        for (Iterator i = integrantesTable.getItemIds().iterator(); i.hasNext();) {
            System.out.println("sdfasdfasfasfdasdf");
            // Get the current item identifier, which is an integer.
            int iid = (Integer) i.next();
            
            // Now get the actual item from the table.
            Item item = integrantesTable.getItem(iid);
            
            // And now we can get to the actual checkbox object.
            items = (String)(item.getItemProperty("legajo").getValue());
                        
           // items += item.getItemProperty("legajo").getValue() + " ";
            Notification.show(items);
            System.out.println(items);
        }
        }
    });
}

private Table setintegrantetable(final Table integrantesTable, final int cont) {
try {
// Set the data type
final ComboBox legajo = new ComboBox(null);
final ComboBox nombre = new ComboBox(null);
ComboBox categoria = new ComboBox(null);
ComboBox escalafon = new ComboBox(null);
CheckBox responsable = new CheckBox(null);

        // Set a number for the table and cast to string for asign to a Label component
        final Label numA = new Label(Integer.toString(cont));

        // Get the comboboxes data for the DB.
        FillcombosTablas(legajo, nombre, categoria, escalafon);

        setLegajo(legajo);
        setCategoria(categoria);
        setNombre(nombre);
        setEscalafon(escalafon);
        setResponsable(responsable);

        // add beans to de beancontainer
         beans.addBean(new RH_Empleado(getLegajo(),    getNombre(), getCategoria(),
                                        getEscalafon(), getResponsable(), cont));    
        
        // Bind the table to the beancontainer
        integrantesTable.setContainerDataSource(beans);
        
        try {
            // generate a colum in the table with a title
            integrantesTable.addGeneratedColumn("Borrar",
                new Table.ColumnGenerator() {
                    public Object generateCell(Table source, final Object itemId, Object columnId) {
                        Button removeButton = new Button("X");
                        // create a button listener of a button to delete
                        // that line according to the id in the idemId
                        removeButton.addClickListener(new ClickListener() {
                        public void buttonClick(ClickEvent event) { integrantesTable.removeItem(itemId); }
                    });
                    return removeButton;
                }
            });
        } catch (Exception e) {
            System.out
                    .println("error en boton borrar de tablasintegrantes");
            e.printStackTrace();
        }
        return integrantesTable;

    } catch (Exception e) {
        System.out.println("error En rellenartablasIntegrantes");
    }
    return integrantesTable;
}

Can you show the code of your class RH_Empleado?

@SuppressWarnings(“serial”)
public class RH_Empleado implements Serializable {

@PropertyId("legajo")
@NotNull
private ComboBox legajo;
private ComboBox nombre;
private ComboBox categoria;
private int numero;
CheckBox  responsable  = null;
private ComboBox escalafon;
    

public RH_Empleado(ComboBox legajo, ComboBox nombre, ComboBox categoria,
        ComboBox escalafon, CheckBox responsable, int cont) {
    this.legajo   = legajo;
    this.nombre = nombre;
    this.categoria   = categoria;
    this.escalafon   = escalafon;
    this.responsable   = responsable;
    this.numero   = cont;
    }



public ComboBox getLegajo() { return this.legajo; }
public void setLegajo(ComboBox legajo) { this.legajo = legajo; }

public ComboBox getNombre() {    return this.nombre; }
public void setNombre(ComboBox nombre) {    this.nombre = nombre;    }

public ComboBox getCategoria() { return this.categoria;    }
public void setCategoria(ComboBox categoria) {    this.categoria = categoria;    }

public ComboBox getEscalafon() {    return this.escalafon; }
public void setEscalafon(ComboBox escalafon) { this.escalafon = escalafon; }

public CheckBox getResponsable() { return responsable;    }
public void setResponsable(CheckBox responsable) {    this.responsable = responsable;    }

public int getNumero() {    return numero;    }
public void setNumero(int numero) {    this.numero = numero;    }

}

I have still the same problem, I implement a listener in FormSolComis and can capture all the form data, except data tables to print the chart shows me something like this com.vaadin.ui.CheckBox@450a5ad5

//listeners for the buttons in this form
    completar.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {    Escuchas(event);    }
    });

    continuar.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {    Escuchas(event);    }
    });

    abortarTarea.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {    Escuchas(event);    }
    });

    abortarProceso.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {    Escuchas(event);    }
    });
}


private void Escuchas(ClickEvent event){

    //this work fine

    String urgCmb = urgenteCombo.getValue().toString();    // Urgente Combo
    String cDiasTxt = cantDiasTxt.getValue().toString();// Cant DiasInt
    String tm = tipoMovilidadCombo.getValue().toString();// Tipo de Movilidad valores BD
    String vac = valesCombo.getValue().toString();// Vales Valores Si No
    String vec = vehiculoCombo.getValue().toString();// Vehiculo
    String ni = numeroInstancia.getValue();// Numero de instancia
    Date feSa = fechaSalidaDateField.getValue();// Fecha de salida
    Date feSo = fechaSolicitud.getValue();// Fecha de Solicitud
    String solicitante = solicitanteData.getValue();// Fecha de Solicitud
    String mot = motivoTextArea.getValue();// Motivo
    


    //this fails y can capture de data but prints mome this like this com.vaadin.ui.CheckBox@450a5ad5
    for (Object itemId : integrantesTable.getItemIds()) {

        final String Vlegajo1 =  String.valueOf(integrantesTable.getItem(itemId).getItemProperty("legajo").getValue());
        System.out.println("Show legajo: - "+ Vlegajo1);
    }
}

When y try to iterate

String items = “”;
for (Iterator i = integrantesTable.getItemIds().iterator(); i.hasNext():wink: {
// Get the current item identifier, which is an integer.
int iid = (Integer) i.next();
System.out.println("Selected items: " + iid);

Y have the next error
java.lang.ClassCastException: model.RH_Empleado cannot be cast to java.lang.Integer

Hi Esteban!

I try to answer you later today, unfortunately I have been a little busy this week :slight_smile:

Thanks again Sara. i really appreciate

Hi!

I suggest that you do not use the BeanItemContainer because your Empleado-class has components like ComboBox and CheckBox, which do not work as parts of Java-beans.

Instead I suggest that you put components inside the table as described in the Vaadin book. In the following chapter you have an example of how to do that:
https://vaadin.com/book/-/page/components.table.html

First you need to specify containerproperties:

table.addContainerProperty(LEGAJO, ComboBox.class, null); table.addContainerProperty(NOMBRE, ComboBox.class, null); table.addContainerProperty(CATEGORIA, ComboBox.class, null); table.addContainerProperty(ESCALAFON, ComboBox.class, null); table.addContainerProperty(RESPONSABLE, CheckBox.class, null); table.addContainerProperty(BORRAR, Button.class, null); table.addContainerProperty(NUMERO, Label.class, null); Second you have a button that adds row to the table:

[code]

button.addClickListener(new Button.ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            cont++;
            addrow(cont);
        }
    });

[/code]The method addrow() adds a row the the table:

[code]
private void addrow(final int cont) {

    final ComboBox legajo = new ComboBox("legajo");
    final ComboBox nombre = new ComboBox("nombre");
    final ComboBox categoria = new ComboBox("categoria");
    final ComboBox escalafon = new ComboBox("escalafon");
    final CheckBox responsable = new CheckBox("responsable");
    final Button borrar = new Button("borrar");
    final Label numero = new Label(cont + "");

    // fill values to comboboxes and checkbox
    fillBoxes();

    // add all the elements inside an object-array to the table. you need
    // also to specify an id for the row, we use now the int-value cont
    table.addItem(new Object { legajo, nombre, categoria, escalafon,
            responsable, borrar, numero }, cont);

[/code]The button borrar will remove a specific row from the table. This code is inside method addrow()

[code]
borrar.addClickListener(new Button.ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {

            // we remove a row-item with id cont from the table
            table.removeItem(cont);
        }
    });
}

[/code]To read values from specific rows, you need to know the correct rownumber. You get values from rows like this:

ComboBox legajoBox = (ComboBox) table.getItem(cont).getItemProperty(LEGAJO).getValue();
CheckBox responsableBox = (CheckBox) table.getItem(cont).getItemProperty(RESPONSABLE).getValue();

String legajoBoxValue = legajoBox.getValue();
boolean responsableBoxValue = responsableBox.getValue();

I believe that your code will work after you make these modifications :slight_smile:

Thanks again Sara Seppola. works perfect.
I appreciate it a lot.