In my JTable, all my checkboxes are in column 3. after I onclick a button, it will remove those rows that are checked.
But however after implementing the functions in my onclick. It does not remove the the rows.
The method for the button listener
class DeleteBtnListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
for(int row = 0; row < table.getRowCount(); ++row) {
if((Boolean) table.getValueAt(row, 3) == true) {
myTableModel.removeRow(row);
table.revalidate();
table.repaint();
}
}
}
}
and this is what is in my AbstractTableModel class
@SuppressWarnings("serial")
class MyTableModel extends AbstractTableModel {
private final boolean DEBUG = true;
private String[] columnNames = {"Name",
"Age",
"Salary",
"Delete"};
private Object[][] data = {
{"Kathy", "20",new Integer(5), new Boolean(false)},
{"John", "35", new Integer(3), new Boolean(false)},
{"Sue", "20", new Integer(2), new Boolean(false)},
{"Jane", "12", new Integer(20), new Boolean(false)},
{"Mary", "42", new Integer(10), new Boolean(false)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
@SuppressWarnings("unchecked")
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value);
}
data[row][col] = value;
fireTableCellUpdated(row, col);
}
public void removeRow(int row) {
fireTableRowsDeleted(row, row);
}
}