Java FX table column sorting with Comparator does not work


Question

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?

1
2
9/2/2013 5:05:02 PM

Accepted Answer

Your problem can be solved by using a CellFactory for the column displaying Kundenwert. This way you don't need the kundenwertFormatted field in your model, since formatting will be done by the cell factory:

public class KundenwertCellFactory implements Callback<TableColumn<ProfilZuordnungTableRowModel, Double>, TableCell<ProfilZuordnungTableRowModel, Double>> {

    public TableCell<ProfilZuordnungTableRowModel, Double> call(TableColumn<ProfilZuordnungTableRowModel, Double> param) {
        TableCell<ProfilZuordnungTableRowModel, Double> cell = new TableCell<ProfilZuordnungTableRowModel, Double>() {

            @Override
            public void updateItem(final Double item, boolean empty) {
                if (item != null) {
                    setText(item.toString()); // you can format your value here
                }
            }
        };
        return cell;
    }
}

Then, you can create your comparator to compare kundenwert which is double:

public class KundenwerComparator implements Comparator<Double> {

    public int compare(Double o1, Double o2) {
        return o1 < o2 ? -1 : o1 == o2 ? 0 : 1;

    }

}

Finally, you can setup your column as follows:

TableColumn<ProfilZuordnungTableRowModel, Double> kundenwertColumn = new TableColumn<ProfilZuordnungTableRowModel, Double>();
kundenwertColumn.setText("Kundenwert");

kundenwertColumn.setCellValueFactory(new PropertyValueFactory<ProfilZuordnungTableRowModel, Double>("kundenwert"));
kundenwertColumn.setComparator(new KundenwerComparator());
kundenwertColumn.setCellFactory(new KundenwertCellFactory());

Note that I used type arguments since TableColumn (and it's companions) is generic.

5
9/3/2013 9:59:27 PM

Your comparator for kundenwertColumn should implement a Comparator for Strings. It goes like

class KundenwertFormattedComparator implements Comparator<String> {
    @Override
    public int compare(String t, String t1) {
         return getKundenwertFromFormatted(t) < getKundenwertFromFormatted(t1)  ? -1 : getKundenwertFromFormatted(t) == getKundenwertFromFormatted(t1) ? 0 : 1;
    }
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon