Skip to content
Advertisement

Invert the Selection in JTable

On clicking a button, I want the selected rows to be inverted (non-selected rows should be selected and selected rows should be non-selected).

Is there a build-in method in JTable to do it?

Advertisement

Answer

JTable doesn’t seems to have a built-in way of doing this. So I implemented it with the following code. (Hope this is helpful for someone who is facing a similar issue.)

int[] selectedIndexs = jtable.getSelectedRows();
jtable.selectAll();

for (int i = 0; i < jtable.getRowCount(); i++) {
    for (int selectedIndex : selectedIndexs) {
        if (selectedIndex == i) {
            jtable.removeRowSelectionInterval(i, i);
            break;
        }
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement