import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableTest {
    public static void main(String[] args) {
        DefaultTableModel model = new DefaultTableModel(0, 1);
        model.addRow(new Object[]{0});
        model.addRow(new Object[]{1});
        model.addRow(new Object[]{2});
        JTable table = new JTable(model);
        table.setAutoCreateRowSorter(true);
        table.setRowSelectionInterval(2, 2);
        System.out.println(Arrays.toString(table.getSelectedRows()));

        RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
		@Override
		public boolean include(Entry<? extends Object, ? extends Object> entry) {
			Object id = entry.getIdentifier();
			return id instanceof Integer && ((Integer) id) != 0;
		}
		};
        ((DefaultRowSorter<?, ?>) table.getRowSorter()).setRowFilter(filter);

        model.insertRow(2, new Object[]{"x"});
        System.out.println(Arrays.toString(table.getSelectedRows()));
        if (table.getSelectedRowCount() != 1) {
            throw new RuntimeException("Test Failed: Selected row count is not 1 after inserting a new row.");
        }
    }
}