pass data from one table to another

0

Good people! I have a Jframe with 2 tables, what I want to do is pass data from one table to the other with a mouseclicked event, which I have achieved, but only one column loads me, that is, if I click with something else from the first table and I load it in the place of the one that was previously selected (in the second table) I leave my code to see if anyone can help me! thank you! []

        int FC = tabla1.getSelectedRow();

          cargartabla2();  

    try{

           String codigo,numero,importe;

        if(FC != -1){
            dtm= (DefaultTableModel)tabla1.getModel();
            codigo = tabla1.getValueAt(FC, 0).toString();
            numero=tabla1.getValueAt(FC, 1).toString();
            importe=tabla1.getValueAt(FC, 2).toString();

            dtm= (DefaultTableModel)tabla2.getModel();
            Object fila[] = {codigo,numero,importe};
            dtm.addRow(fila);


        }

    }catch(Exception e ){

    }
    
asked by Maximiliano Correa 28.08.2017 в 16:02
source

1 answer

0

MODIFIED !!

Create a button and a new operation called passVariableFiles (). You create an event that when pressing the button this function is executed.

DATA! - > To work with the selection without the button you can use the event on the mouseReleased () table.

private void pasarVariasFilas (){
        //Esto es nuevo. Recoge todas las filas seleccionadas. 
        int FC[] = tabla1.getSelectedRows();
        DefaultTableModel dtm;
        String codigo,numero,importe;
        //Recorremos todas las filas seleccionadas para cargar los datos.
        for (int i = 0; i < FC.length; i++) {
                //Obtenemos fila por fila.
                int j = FC[i];

                dtm= (DefaultTableModel)tabla1.getModel();
                codigo = tabla1.getValueAt(j, 0)+"";
                numero=tabla1.getValueAt(j, 1)+"";
                importe=tabla1.getValueAt(j, 2)+"";

                if (codigo.equals("null")) {
                    codigo = "";
                }
                if (numero.equals("null")) {
                    numero = "";
                }
                if (importe.equals("null")) {
                    importe = "";
                }


                boolean todoVacio = false;
                if (codigo.isEmpty() &&
                    numero.isEmpty() &&
                    importe.isEmpty()) {
                    todoVacio = true;
                }
                if (!todoVacio) {
                    dtm= (DefaultTableModel)tabla2.getModel();
                    Object fila[] = {codigo,numero,importe};
                    dtm.addRow(fila);
                }

        }

    }

I hope it solves your doubt.

    
answered by 28.08.2017 / 16:47
source