0

I just wanna ask, how to detect a checked box on check boxes and how to update the table or delete the row of the checked box? i run this code and it exits the loop and didn't enter the if else statement :(

for(int i =table.getRowCount(); i>= 0 ; i++)
{
    a = (String)table.getModel().getValueAt(i,1);
    boolean getBool = (boolean)table.getModel().getValueAt(i, 6);
    if(getBool)
    {
        JOptionPane.showMessageDialog(null,getBool); 
        String sql2 = "UPDATE room_record SET Book_status = 'Checked_In' WHERE
        Room_Record_ID = '" + a + "'";
        statement = conn.prepareStatement(sql2);
        statement.executeUpdate(sql2);
        ((DefaultTableModel) table.getModel()).removeRow(i)
        //model2.fireTableDataChanged();
        JOptionPane.showMessageDialog(null,"done"); 
        //System.out.println("true");
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Michimcchicken
  • 543
  • 1
  • 6
  • 21

1 Answers1

2

how to detect a checked box

Create a loop to read through the TableModel. Then use:

if (model.getValueAt(...).equals(Boolean.TRUE))
    // do something

delete the row of the checked box?

Use the removeRow(...) method of the DefaultTableModel

Note when you loop through the TableModel you will need to start with the last entry and decrement your index by 1 until you get to 0 so the removeRow() method will work on the proper row.

Edit:

Did you add a System.out.println(...) statement inside your loop to make sure the code was executing? This is a simple debugging technique to test your logic. Your code won't execute because your looping structure in incorrect.

int i =table.getRowCount(); 

You can't start at the row count because Java starts offsets at zero so you need to use:

int i =table.getRowCount() - 1; 

I then suggested you need to decrement the index. Why are you using:

i++

That does an increment, not a decrement.

camickr
  • 321,443
  • 19
  • 166
  • 288