Question: I want to keep all the selected rows in the jtable and delete the rest of them. I found many answers regarding how to delete the selected rows but how can I delete the non selected rows only. Please help

Answer: 
A couple of tips:

1. Don't worry about the array of selected rows. The indexes of the selected rows is automatically adjusted as your add/remove a row from the model. Just start deleting rows from the end checking the selected state of each row as you go.
2. Convert the view index to the model index in case the table is sorted or filtered.

Then the basic code is:

```
DefaultTableModel model = (DefaultTableModel)table.getModel();

for (int i = model.getRowCount() -1; i >=0; i--)
{
    if (! table.isRowSelected(i))
        model.removeRow( table.convertRowIndexToModel(i) );
}

```
I could not test it at all, but i hope it helps:

```
public void removeSelectedRows(JTable table){
    DefaultTableModel model = (DefaultTableModel) this.table.getModel();
    int[] rows = table.getSelectedRows();
    int rowsCount = table.getRowCount();
    int deletedRows = 0;
    for(int i=rowsCount-1;i>-1;i--){
        if(i!=rows[rowsCount-1-deletedRows]){ //Check non-selected rows
            model.removeRow(i);
            deletedRows++;
        }
    }
}

```

Maybe there is an easiest way but this should work fine.