Validate a CheckBox in a JTable - Java

0

I have placed a checkbox in a JTable . The problem is that when I click on the test button ( btnPrueba ), a validation message "marque" appears. How would I validate correctly ?:

protected void btnPruebaActionPerformed(ActionEvent e) {
    for(int i = 0; i < tbDocNoUbicado.getRowCount(); i++){
        Boolean checked = Boolean.valueOf((boolean) tbDocNoUbicado.getValueAt(i, 13));
        String col = (String) tbDocNoUbicado.getValueAt(i, 0);

        if(checked == true){
            System.out.println(col);
        }
        else{
            JOptionPane.showMessageDialog(null, "marque");
            return;
        }
    }
}
    
asked by Angelica 18.05.2017 в 18:23
source

1 answer

0

Taking into account that the 13 column is the correct one and that it contains the checkbox , you could choose to make a cast different from the value of the column, accessing the method toString ()

Boolean checked = Boolean.valueOf(tbDocNoUbicado.getValueAt(i,13).toString());
if(checked){ ...}
else {....}

One way to validate is to use an auxiliary variable to count the amount of check values checked

   int cont =0;  /* Var Auxiliar*/
   Boolean checked = false;
   for(int i = 0; i < jTable1.getRowCount(); i++){
        checked = Boolean.valueOf(jTable1.getValueAt(i,1).toString());
        /* Si está marcado , aumentamos el valor*/
        if(checked){
          cont++;
          break;
        } 
   }
   if(cont==0){ /* si es 0 , es porque no hay opciones marcadas*/
       JOptionPane.showMessageDialog(null, "marque");
   }
    
answered by 18.05.2017 / 18:45
source