An inconvenience occurs when recording multiple rows of a jtable

0

I have a Jtable that contains rows inserted from a database, the last column is a checkbox. This is the code to record the row every time I click on the checkbox:

protected void btnEnviarActionPerformed(ActionEvent e) {
        DefaultTableModel model = (DefaultTableModel) tbDocSolicitud.getModel();
        int[] selectedRow = tbDocSolicitud.getSelectedRows();


        for(int t : selectedRow){
            if((Boolean)model.getValueAt(t, 14) == true){
                String codArchivo = (String) tbDocSolicitud.getValueAt(t, 0);
                String codCentroCosto = (String) tbDocSolicitud.getValueAt(t, 1);
                String tipoArchivo = (String) tbDocSolicitud.getValueAt(t, 4);
                String doc = (String) tbDocSolicitud.getValueAt(t, 6);
                String nivelArchivo = (String) tbDocSolicitud.getValueAt(t, 7);
                String lote = (String) tbDocSolicitud.getValueAt(t, 9);
                String fila = (String) tbDocSolicitud.getValueAt(t, 10);
                String usuarioSolicita = Constante.idUsuario;
                String fecha = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
                String fechaSolicita = fecha;
                Objeto object = (Objeto) cboCentroCostoDestino.getSelectedItem();
                String centroCostoDestino = ((Objeto)object).getCodigo();
                String centroCostoOrigen = Constante.c_ccosto;
                if (centroCostoDestino.equals("0")){
                    JOptionPane.showMessageDialog(null, "Seleccione el Destino del Documento","Alerta",JOptionPane.WARNING_MESSAGE);
                    cboCentroCostoDestino.requestFocus();
                }
                else{
                    MovimientoArchivoDTO m = new MovimientoArchivoDTO();
                    m.setC_c_archivo(codArchivo);
                    CentroCostoDTO c = new CentroCostoDTO();
                    c.setC_ccosto(codCentroCosto);
                    m.setC_ccosto(c);
                    m.setC_ccosto_origen(centroCostoOrigen);
                    m.setC_ccosto_destino(centroCostoDestino);
                    m.setC_tipo_doc(tipoArchivo);
                    m.setC_t_doc(doc);
                    m.setC_c_nivel_archivo(nivelArchivo);
                    m.setLote(lote);
                    m.setFila(fila);
                    m.setC_c_usuario_solicita(usuarioSolicita);
                    m.setD_dt_solicita(fechaSolicita);
                    int estado = x.RegistrarSolicitud_SA(m);
                    if (estado == 1){
                        JOptionPane.showMessageDialog(null,doc);            
                    }
                    else
                        mensaje("Error en enviar");
                }
            }
        }
        ListarDocumentoSA(Constante.c_ccosto, estado_flg);
    }

When I click on the checkbox of ONE row and I click on the record button if it is recorded, but the problem is that when I click on the checkbox of THREE or MORE rows, only one is saved and no keep the ones that have the check in the box.

    
asked by Leandro 15.03.2017 в 18:03
source

1 answer

0

JTable get the value checkbox when checkbox is checked

table.getModel().addTableModelListener(new TableModelListener() {
              @Override
              public void tableChanged(TableModelEvent e) {
                   for(int i=0;i<table.getModel().getRowCount();i++)
                      {
                        if ((Boolean) table.getModel().getValueAt(i,0))
                        {  
                          System.out.println(">\t"+table.getSelectedRow());
                          break;
                        }
                     }     
                  }
        });

I also found a very well explained example Here

    
answered by 15.03.2017 / 22:55
source