Hammad Ali Khan
Hammad Ali Khan

Reputation: 138

remove a selected checkbox row(single and multiple) in JTable

I want to remove row(single or multiple both) whose checkbox is selected in jtable through a button but it is not working properly..it is throwing exceptions.. here is the code..

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
        for (int i=0;i<model.getRowCount();i++) {
              Boolean checked=(Boolean)model.getValueAt(i,7);
              if (checked) {
                   model.removeRow(i);
                   i--;
              }
        }
}        

Upvotes: 0

Views: 1915

Answers (3)

A.YULGHUN
A.YULGHUN

Reputation: 11

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        DefaultTableModel tm= (DefaultTableModel) jTable1.getModel();

    for (int i=0;i<tm.getRowCount();i++) {
          Boolean checked=(Boolean)model.getValueAt(i,0);
          if (checked!=null && checked) {
               tm.removeRow(i);
               i--;
          }
    }
    } 

Upvotes: 0

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9872

that's because if combobox isn't selected you will get null not false.to avoid this you can check null or not first

DefaultTableModel model = (DefaultTableModel) jTable2.getModel();

    for (int i=0;i<model.getRowCount();i++) {
          Boolean checked=(Boolean)model.getValueAt(i,7);
          if (checked!=null && checked) {
               model.removeRow(i);
               i--;
          }
    }

Upvotes: 1

Ezequiel
Ezequiel

Reputation: 3592

Try to iterate in a descent way:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
    for (int i=model.getRowCount();i<0;i--) {
          Boolean checked=(Boolean)model.getValueAt(i-1,7); // Checkbox is in 7th column?
          if (checked) {
               model.removeRow(i-1);
          }
    }
}  

Upvotes: 0

Related Questions