adding Jtable cells

-1

Good day, I have a small problem, I have 2 tables in the same panel, one loads pending invoices, by clicking on them they are added to the Janda seguanda, until then everything is perfect, now I have a Jlabel that I need show me the amount that is added in the second table, that is to say as I click on the first table the data of the amount of the invoices that are going to the second table are added in that Jlabel. I tried some codes and he gave me 0 all the time. Some help? Something important is that the column amount has a numberformat.Muchas Thank you colleagues!

    
asked by Maximiliano Correa 31.08.2017 в 14:47
source

2 answers

1

We continue with the same example of the other answer. Hope this can help you.

private float sumarImporte(){
        DefaultTableModel dtm;
        //LA VARIABLE QUE ALMACENARA EL TOTAL.
        float importeTotal = 0;
        //RECORREMOS TODAS LAS FILAS PERO EN ESTA OCASION SUMAMOS SOLO
        // LA QUE TIENE EL IMPORTE. 
        for (int i = 0; i < tabla2.getRowCount(); i++) {
            //LLAMAMOS EL MODELO DE LA TABLA.
            dtm = (DefaultTableModel)tabla2.getModel();
             //HACEMOS LA CONVERSION CON UN PARSE PARA QUE NO DE ERROR. 
            // EN ESTA PARTE HAY QUE TENER CUIDADO DE QUE LA CELDA 
            // NO CONTENGA LITERALES PUES NOS MANDARA ERROR. 

            //ESTA ES LA NUEVA LINEA.
            String a = tabla2.getValueAt(i, 2)+"";
            a = a.replace(".", "");
            a = a.replace(",", ".");


            float importe = Float.parseFloat(a);
            //SUMAMOS LA FILA ACTUAL AL TOTAL GLOBAL.  
            importeTotal+=importe;
        }
        //RETORNAMOS EL RESULTADO DE LA SUMA. 
        return importeTotal;
    }

UPDATE

Change the format you get from the table with these lines:

            String a = tabla2.getValueAt(i, 2)+"";
            a = a.replace(".", "");
            a = a.replace(",", ".");
    
answered by 31.08.2017 в 15:29
0

This would be the basic idea of what you want to do:

Double valores=new Double(0.0);
for (int i = 0; i < tabla2.getRowCount(); i++) {
    Double valor = new Double( tabla2.getValueAt(i, 2).toString());
    valores += valor;
}
etiqueta.setText(valores.toString());
    
answered by 31.08.2017 в 15:19