I've tried this:
public void removeSelectedFromTable(JTable from)
{
    int[] rows = from.getSelectedRows();
    TableModel tm= from.getModel();
    while(rows.length>0)
    {
        ((DefaultTableModel)tm).removeRow(from.convertRowIndexToModel(rows[0]));
        rows = from.getSelectedRows();
    }
    from.clearSelection();
}
But, it sometimes leaves one still there. What can be the problem?
If using the DefaultTableModel , just set the row count to zero. This will delete the rows and fire the TableModelEvent to update the GUI. JTable table; … DefaultTableModel model = (DefaultTableModel) table.
So you can call table. getSelectionModel(). isSelectionEmpty() to find out if any row is selected.
It doesn't work, this is better:
public void removeSelectedRows(JTable table){
   DefaultTableModel model = (DefaultTableModel) this.table.getModel();
   int[] rows = table.getSelectedRows();
   for(int i=0;i<rows.length;i++){
     model.removeRow(rows[i]-i);
   }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With