0

this is more or less a duplicate of the ones below:
How can i sort java JTable with an empty Row and force the Empty row always be last?
http://www.java.net/node/645655

the only difference is that i would like to insert the new row after the row that is currently selected. the default behavior of DefaultRowSorter would sort immediately when the row is inserted, while what i need is to turn off sorting temporarily so that i could insert the row in a position that i specified, not a position from the sorter

i noticed that there were 2 links provided in How can i sort java JTable with an empty Row and force the Empty row always be last?, but they are not valid anymore, that's why i created this new question.

Community
  • 1
  • 1
xu huanze
  • 269
  • 2
  • 9

1 Answers1

0

I copied completely DefaultRowSorter and TableRowSorter from JDK and changed insertInOrder() like below and it seems now working as I expected.

for (int i = 0; i < max; i++) {
if(sortOnInsert)
{
    index = Arrays.binarySearch(current, toAdd.get(i));
    if (index < 0) {
        index = -1 - index;
    }
}
else
{
    Row rowBeforeAdd = new Row(this, toAdd.get(i).modelIndex - 1);
    index = Arrays.binarySearch(current, rowBeforeAdd);
    if (index < 0) {
        index = viewToModel.length - 1;
    }
    else
    {
        index += 1;
    }
}
xu huanze
  • 269
  • 2
  • 9