I have a simple TableView (Java FX 2.0, but I suppose the problem is fairly generic) that gets the default sorting feature. However the table has a total in its last line so I would like to exclude that last line from the sorting algorithm.
I found a solution for a Swing JTable that consists in creating a separate table for the total row - that would be transposable to a TableView but it seems a bit cumbersome. I have tried implementing my own Comparator but I don't think it is possible to create one that would work in both ascending & descending orders.
And if you always return 0 in your Comparator's compareTo() method? It means that all elements "are the same" and no swapping of places should occur?
With JavaFX 8 it is possible to define a sorting policy, and the problem is easier to fix. Assuming the row containing the totals is
TOTAL
:Based on lolsvemir's answer I successfully implements a "TOTAL" with a custom Comparator. I set the comparator with column's sortTypeProperty:
col.setComparator(new BigDecimalColumnComparator(col.sortTypeProperty()));
Custom comparator:
I had the same issue and, due to TableView property to call cell factory for as many rows as are visible in the table, and not only for row indexes up to the size of list set by TableView.setItems() method, IMO it is better to set summary value directly by cell factory for every column which needs to have it.
Here is the simple example of such cell factory class:
Advantages of this approach are that such row is not involved in sorting at all (because it is not in the table items list) and not selectable also. Additionally, with overriding TableRow factory is easy to set different background color for whole summary row and to distinguish it from other rows.
However, this additional row will not be visible while scrolling down because TableView scrolling implementation does not know for the additional row and will scroll only to the last row from the items list at the bottom of the viewport. That can be solved by overriding TableViewSkin.getItemCount() method, like in the following example. Overriding TableView itself is not necessary, but is recommended if such table is used often.
This may look like a hack, but works like a charm.