Select multiple alternate rows in a JTable

1

At the moment I select a row and I click on the send button, it is recorded correctly but when I select several rows, only one row is recorded, as I would do so that all the rows I selected will be recorded. I share an image and the code with which I record and select the rows.

protected void btnEnviarActionPerformed(ActionEvent arg0) {
        //se crea una variable para que contenga la fila seleccionada de la tabla
        int i = tbDocTransferir.getSelectedRow();
        //hacemos una condicion de que si la varialbe i es -1 es que no se ha seleccionado ninguna 
        //fila
        if (i == -1){
            JOptionPane.showMessageDialog(null,"Seleccione la fila que desea solicitar"); 
        }
        else{
            String codArchivo = (String) tbDocTransferir.getValueAt(i, 0);
            String codCentroCosto = (String) tbDocTransferir.getValueAt(i, 1);
            String tipoArchivo = (String) tbDocTransferir.getValueAt(i, 4);
            String doc = (String) tbDocTransferir.getValueAt(i, 6);
            String nivelArchivo = (String) tbDocTransferir.getValueAt(i, 8);
            String lote = (String) tbDocTransferir.getValueAt(i, 10);
            String fila = (String) tbDocTransferir.getValueAt(i, 11);
            String usuarioSolicita = txtUsuarioSolicita.getText();
            String fechaSolicita = txtFechaSolicita.getText();
            Objeto object = (Objeto) cboCentroCostoDestino.getSelectedItem();
            String centroCostoDestino = ((Objeto)object).getCodigo();
            String centroCostoOrigen = txtCodCentroCosto.getText();

            MovimientoArchivoDTO x = new MovimientoArchivoDTO();
            x.setC_c_archivo(codArchivo);
            x.setC_ccosto(codCentroCosto);
            x.setC_ccosto_origen(centroCostoOrigen);
            x.setC_ccosto_destino(centroCostoDestino);
            x.setC_tipo_doc(tipoArchivo);
            x.setC_t_doc(doc);
            x.setC_c_nivel_archivo(nivelArchivo);
            x.setLote(lote);
            x.setFila(fila);
            x.setC_c_usuario_solicita(usuarioSolicita);
            x.setD_dt_solicita(fechaSolicita);
            int estado = xy.RegistrarSolicitudArchivo_SA(x);
            if (estado == 1){
                mensaje("El Documento se solicitó correctamente");
            }
            else
                mensaje("Error en enviar");
        }
    }

    
asked by Bruno 26.01.2017 в 22:22
source

2 answers

1

A% JTable works in MULTIPLE_INTERVAL_SELECTION mode if you do not change it. The following modes exist:

  • ListSelectionModel.SINGLE_SELECTION - You can select only one index
  • ListSelectionModel.SINGLE_INTERVAL_SELECTION - you can select consecutive indexes
  • ListSelectionModel.MULTIPLE_INTERVAL_SELECTION - you can select without restriction

What happens is that you use tabla.getSelectedRow() that only returns the number of the last selected row. what you are looking for is probably:

int[] seleccionados = tabla.getSelectedRows();
    
answered by 26.01.2017 / 22:58
source
1

I hope it serves you.

class SharedListSelectionHandler implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent e) { 
        ListSelectionModel lsm = (ListSelectionModel)e.getSource();

        int firstIndex = e.getFirstIndex();
        int lastIndex = e.getLastIndex();
        boolean isAdjusting = e.getValueIsAdjusting(); 
        output.append("Event for indexes "
                      + firstIndex + " - " + lastIndex
                      + "; isAdjusting is " + isAdjusting
                      + "; selected indexes:");

        if (lsm.isSelectionEmpty()) {
            output.append(" <none>");
        } else {
            // Find out which indexes are selected.
            int minIndex = lsm.getMinSelectionIndex();
            int maxIndex = lsm.getMaxSelectionIndex();
            for (int i = minIndex; i <= maxIndex; i++) {
                if (lsm.isSelectedIndex(i)) {
                    output.append(" " + i);
                }
            }
        }
        output.append(newline);
        output.setCaretPosition(output.getDocument().getLength());
    }
}

More information: How to Write a List Selection Listener

    
answered by 26.01.2017 в 22:46