I have a JTable
where the last row is the total row that aggregates all other rows. When the user clicks a column header on the table, rows are sorted by that column, except for the total row which should be always at bottom.
Is there a simple way to implement this with TableRowSorter
?
The following solution worked for me....
In your table model update the getRowCount()
member to return 1 row less than required.
Then modify the index and row counts reported by your sorter as follows...
TableRowSorter<TableModel> sorter =
new DefaultTableRowSorter<TableModel>(this.getModel())
{
public int convertRowIndexToModel(int index)
{
int maxRow = super.getViewRowCount();
if (index >= maxRow)
return index;
return super.convertRowIndexToModel(index);
}
public int convertRowIndexToView(int index)
{
int maxRow = super.getModelRowCount();
if (index > maxRow)
return index;
return super.convertRowIndexToView(index);
}
public int getViewRowCount()
{
return super.getViewRowCount() + 1;
}
};
myTable.setRowSorter(sorter);