Problem showing the data in a JTable

0

I have the following problem, I was developing a code that would do some calculations with distributions and random numbers that would then be passed to a table in another view that would show the results of the n repetitions of the calculation, but when I review the results of the table , I see that you are repeating the same values the number of times you have to repeat the calculations, check that you are not getting the same result always and everything is correct, so my problem is in the way of sending and / or printing the data in the table.

The calculation code is this:

Cant_Rep = Integer.parseInt(Repeticiones.getText());
        Hec_Tot = 3;
        Kil_Hec = 9000;
        Porc_Ferm = new dist_Uniforme(60,70);
        Porc_Sobr = new dist_Uniforme(0,20);
        Porc_Desc = new dist_Normal(3.575,18.0625);
        Lit = 750;
        Costo_Fijo = 15600;
        Costo_Jov = 2.10;
        Costo_Añe = 7.10;
        Costo_Esp = 3.10;
        Precio_Jov = 95;
        Precio_Añe = 450;
        Precio_Esp = 120;
        Demanda_Jov = new dist_Triangular(9000,11000,11800);
        Demanda_Añe = new dist_Triangular(2100,2500,4100);
        Demanda_Esp = new dist_Normal(0.81,0.661);

        Hec_Util = 3;
        Tiem_Ferm = 8;
        Porc_Cons = 60;

        vr = new vent_resultado();
        vr.setVisible(true);

        int $i = 0;

        while($i<=Cant_Rep)
        {                
            double ps = Porc_Sobr.generarDU();
            double pf = Porc_Ferm.generarDU();
            double pd = Porc_Desc.generarDN();
            Viñedo = (Kil_Hec * Hec_Util) * (ps * 0.01);
            Descarte = Viñedo * (pd * 0.01);
            Seleccion = Viñedo - Descarte;
            Fermentado = Seleccion * (pf * 0.01) * Tiem_Ferm;    
            Ferm_Espum = Seleccion * (pf * 0.01) * (Tiem_Ferm * 2);
            Maduracion = Fermentado * (Lit * 0.01);
            Mad_Espum = Ferm_Espum * (Lit * 0.01);
            Vinos_Jov = Maduracion;
            Vinos_Añe = Maduracion * (Porc_Cons * 0.01);
            Vinos_Esp = Mad_Espum * (Porc_Cons * 0.01);

            double de = Demanda_Esp.generarDN();
            double dj = Demanda_Jov.generarDT();
            double da = Demanda_Añe.generarDT();
            Ing_Jov = Vinos_Jov * Precio_Jov * dj;
            Ing_Añe = Vinos_Añe * Precio_Añe * da;
            Ing_Esp = Vinos_Esp * Precio_Esp * de;
            Ing_Totales = Ing_Jov + Ing_Añe + Ing_Esp;
            Gastos_Jov = Vinos_Jov * Costo_Jov;
            Gastos_Añe = Vinos_Añe * Costo_Añe;
            Gastos_Esp = Vinos_Esp * Costo_Esp;
            Gastos_Tot = Gastos_Jov + Gastos_Añe + Gastos_Esp + Costo_Fijo;
            Ganan_Jov = Ing_Jov - Gastos_Jov;
            Ganan_Añe = Ing_Añe - Gastos_Añe;
            Ganan_Esp = Ing_Esp - Gastos_Esp;
            Ganan_Tot = Ing_Totales - Gastos_Tot;

            vr.mostrarDatos(Viñedo,Descarte,Seleccion,Fermentado,Ferm_Espum,Maduracion,Mad_Espum,Vinos_Jov,Vinos_Añe,Vinos_Esp,Ing_Jov,Ing_Añe,Ing_Esp,Ing_Totales,Gastos_Jov,Gastos_Añe,Gastos_Esp,Gastos_Tot,Ganan_Jov,Ganan_Añe,Ganan_Esp,Ganan_Tot,Cant_Rep);
            $i++;
        }
        this.dispose();

And the one in the table is this:

public void mostrarDatos(double viñe,double desc,double sele,double fer,double fer_esp,double mad,double mad_esp,double vin_jov,double vin_añe,double vin_esp,double ing_jov,double ing_añe,double ing_esp,double ing_tot,double gas_jov,double gas_añe,double gas_esp,double gas_tot,double gan_jov,double gan_añe,double gan_esp,double gan_tot, int cant_rep){

    int con = 0;
    String data [][] = {};
    String col[] = {"#","Viñedo","Descarte","Seleccion","Fermentado","Ferm_Espum","Maduracion","Mad_Espum","Vinos_Jov","Vinos_Añe","Vinos_Esp","Ing_Jov","Ing_Añe","Ing_Esp","Ing_Totales","Gastos_Jov","Gastos_Añe","Gastos_Esp","Gastos_Tot","Ganan_Jov","Ganan_Añe","Ganan_Esp","Ganan_Tot"};
    mtabla = new DefaultTableModel(data,col);
    Datos.setModel(mtabla);

    int $i = 1;
    while ($i<=cant_rep)
    {
        mtabla.insertRow(con, new Object[]{});
        mtabla.setValueAt($i, con, 0);
        mtabla.setValueAt(viñe, con, 1);
        mtabla.setValueAt(desc, con, 2);
        mtabla.setValueAt(sele, con, 3);
        mtabla.setValueAt(fer, con, 4);
        mtabla.setValueAt(fer_esp, con, 5);
        mtabla.setValueAt(mad, con, 6);
        mtabla.setValueAt(mad_esp, con, 7);
        mtabla.setValueAt(vin_jov, con, 8);
        mtabla.setValueAt(vin_añe, con, 9);
        mtabla.setValueAt(vin_esp, con, 10);
        mtabla.setValueAt(ing_jov, con, 11);
        mtabla.setValueAt(ing_añe, con, 12);
        mtabla.setValueAt(ing_esp, con, 13);
        mtabla.setValueAt(ing_tot, con, 14);
        mtabla.setValueAt(gas_jov, con, 15);
        mtabla.setValueAt(gas_añe, con, 16);
        mtabla.setValueAt(gas_esp, con, 17);
        mtabla.setValueAt(gas_tot, con, 18);
        mtabla.setValueAt(gan_jov, con, 19);
        mtabla.setValueAt(gan_añe, con, 20);
        mtabla.setValueAt(gan_esp, con, 21);
        mtabla.setValueAt(gan_tot, con, 22);
        $i++;
        con++;
    }
}

I have not been able to find out where I was wrong, I hope you can explain to me what I did wrong.

    
asked by user85131 12.11.2018 в 06:20
source

1 answer

0

I think I see a fault in the creation of the table, since it can not be compiled in full, try to do this when loading the data. (Code below)

-To start you need to have created a JTable in this case we will call it result. -Now you take your DefaultTableModel and assign it the result model with getModel () -Now we will add each row with the order addRow of table, creating a new array of objects that includes the data for each column.

javax.swing.JTable resultados;
resultados = new javax.swing.JTable();
resultados.setModel(new javax.swing.table.DefaultTableModel (
new Object[][] {},
new String[] {"#","Viñedo","Descarte","Seleccion","Fermentado","Ferm_Espum","Maduracion","Mad_Espum","Vinos_Jov","Vinos_Añe","Vinos_Esp","Ing_Jov","Ing_Añe","Ing_Esp","Ing_Totales","Gastos_Jov","Gastos_Añe","Gastos_Esp","Gastos_Tot","Ganan_Jov","Ganan_Añe","Ganan_Esp","Ganan_Tot"}
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            /*SI DESEAS EDITAR CAMPOS DIRECTAMENTE, PON LAS OPCIONES, EN EL MISMO ORDEN QUE EN EL STRING[] CON TRUE*/
            false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
/*HASTA AQUI HEMOS CREADO LA TABLA CON SUS COLUMNAS Y EL MODELO CON EL ORDEN DE DATOS*/
/*AHORA CREAREMOS OPCIONALMENTE UN SCROLL PANE PARA QUE SE PUEDAN MOVER POR LOS DATOS*/
javax.swing.JScrollPanel panel;
panel = new javax.swing.JScrollPane();
panel.setViewportView(resultados);

/*AHORA INCLUIREMOS LOS DATOS, CAMBIANDO TU WHILE POR UN FOR QUE ES MAS UTIL Y INCLUYENDOLOS CON LA VARIABLE MTABLA*/
mtabla = (DefaultTableModel) resultados.getModel();
for(int i = 1; i <= cant_rep; i++){
    mtabla.addRow(new Object[] {i, viñe, desc, sele, fer, fer_esp, mas, mas_esp, vin_jov, vin_añe, vin_esp, ing_tot, gas_jov, gas_añe, gas_esp, gas_tot, gan_añe, gan_jov, gan_esp, gan_tot});
}
/*Y ASI SE AÑADIRIA CADA DATO*/

In the same way that we change the while for a for, you should do it in the other class. Try that and see if it works for you. Anything you say, a greeting

    
answered by 12.11.2018 в 10:05