Monday, 2 September 2013

Java FX table column sorting with Comparator does not work

Java FX table column sorting with Comparator does not work

In Java FX I would like to display this Model in a sorted TableView:
public class ProfilZuordnungTableRowModel {
private int id;
private double kundenwert;
private String kundenwertFormatted;
private BooleanProperty selected; }
I would like to integrate a table column sorting with the column
"Kundenwert". The displayed value should be the attribute
"kundenwertFormatted" (String) and for sorting the attribute "kundenwert"
(Double) should be used.
So I wrote a comparator:
class ProfilZuordnungTableRowModelComparator implements
Comparator<ProfilZuordnungTableRowModel> {
@Override
public int compare(ProfilZuordnungTableRowModel t,
ProfilZuordnungTableRowModel t1) {
return t.getKundenwert() < t1.getKundenwert() ? -1 :
t.getKundenwert() == t1.getKundenwert() ? 0 : 1;
}
}
In my understanding this comparator should be used in the following way:
TableColumn kundenwertColumn = new TableColumn();
kundenwertColumn.setText("Kundenwert");
kundenwertColumn.setCellValueFactory(new
PropertyValueFactory("kundenwertFormatted"));
kundenwertColumn.setComparator(new
ProfilZuordnungTableRowModelComparator());
But when trying to sort by the column "Kundenwert" I get the following
exception:
java.lang.ClassCastException: java.lang.String cannot be cast to
model.ProfilZuordnungTableRowModel
at
dialog.TableCellFactorySample$ProfilZuordnungTableRowModelComparator.compare(TableCellFactorySample.java:53)
which points to this line:
kundenwertColumn.setComparator(new ProfilZuordnungTableRowModelComparator());
Any ideas?

No comments:

Post a Comment