I am using JTable with an empty row at the bottom of the table in order to give the ability of adding new line to the table.
After insert or writing data in the empty row i am adding automtacly a new empty row below it. (It suppose to act like the Microsoft visual tables)
I am using the java default row sorter,
The problem is that i need the empty row to be the last row all the time! but after sorting the table it become the first row.
The "compare(int model1, int model2)" method of the DefaultRowSorter class is getting 2 row numbers and return -1 if the value of the first row is null and 1 if the value of the second row is null. and incase of DESCENDING it mult by -1 to get the inverted order.
//Treat nulls as < then non-null
if (v1 == null) {
if (v2 == null) {
result = 0;
} else {
result = -1;
}
} else if (v2 == null) {
result = 1;
} else {
result = sortComparators[counter].compare(v1, v2);
}
if (sortOrder == SortOrder.DESCENDING) {
result *= -1;
}
The empty line is sorted as the smallest value and incase of DESCENDING it will be the first line (because of the mult by -1) and causes alot of problems.
I could overide it and incase of the empty row(usualy the last row) do not mult by -1 in DESCENDING mode, and it will be the last row after any sorting. But the problem is that the "compare" method and it's caller "Row" inner class are private inside the DefaultRowSorter.
Is there a way to avoid sorting the empty row and make it always the last row?