Problem with the number of jatable jtable results

0

I'm trying to delete records but I have a problem removing several, because the following code int[] rows = datalistado.getSelectedRows(); only returns one.

This is my code:

   public void leeRifIdentif(){
         String RIF;
         int cont=0;
         modelo = (DefaultTableModel)this.datalistado.getModel();
            int[] rows = datalistado.getSelectedRows();     
            for(int i = 0; i < rows.length; i++){
                RIF=((String)modelo.getValueAt(i, 1));
                cont++;
                 try{
                     ps =cn.prepareStatement("DELETE FROM cliente WHERE rif=?");
                     String rif = String.valueOf(RIF);
                     ps.setString(1,rif);
                     ps.executeUpdate();
                     MostrarDatos(false);
                     VaciarCampos();                              
                    }catch(SQLException e){
                         System.out.println(e.getMessage());
                   }
           }     
            if(cont == (int)rows.length){
                 CboCampo.setSelected(false);
           }
   } 
  

note: it returns only one record and I have selected several

    
asked by Efrainrodc 27.12.2016 в 03:04
source

2 answers

1

As I said before, my solution would be to store all the selected rif. I would do it like this:

1- Make use of the LinkedList class to store the rif

LinkedList<Integer> listaRIF = new LinkedList<>();

create an event to listen to the jtable

 private void jTable1MousePressed(MouseEvent evt) {                                     
    int fila = jTable1.getSelectedRow();
    listaRIF.add(Integer.parseInt((String) jTable1.getValueAt(fila, 0)));
}  

What it will do is capture every time you select a cell.

The following would be: Create a "Delete" button where you will go through all the rifles you have selected in the previous method.

  private void jButton1ActionPerformed(ActionEvent evt) {                                         
    for (int i = 0; i < listaRIF.size(); i++) {
       //haces la eliminacion  como lo planteaste hace un rato
        System.out.println(listaRIF.get(i));

    }

} 
    
answered by 27.12.2016 / 04:06
source
1

I think you have this wrong:

RIF=((String)modelo.getValueAt(i, 1));

It should be

RIF=((String)modelo.getValueAt(rows[i], 1));

With datalistado.getSelectedRows (); you are selecting all MARCHED rows, I mean giving Ctrl and selecting them and I think that you have a checkbox in field one and you want to take those with the selected checkbox, so it should be something like this:

         DefaultTableModel modelo = (DefaultTableModel)this.datalistado.getModel();
        List<Integer> rows=new ArrayList<Integer>();
introducir el código aquí
            //Metemos las filas con el textbox a true en un array
            for(int i=0;i<modelo.getRowCount();i++)
            {
                //Suponiendo que el checkbox es la columna 1(0)
                System.out.println(modelo.getValueAt(i,0));
                if(modelo.getValueAt(i,0)!=null && modelo.getValueAt(i,0).equals(true))
                 rows.add(i);
            }

        for(int i = 0; i < rows.size(); i++){ //Codigo de borrado de cada fila }

Be careful with updating the table before deleting all of them in the database because you can delete other rows than the ones selected so you can:

1.Delete them in the database and delete all update the table. 2.Delete them with a for backwards from the last row selected until the first. 3.Delete them with a forEach.

Greetings.

    
answered by 27.12.2016 в 08:12