问题描述
在Java FX中,我想在已排序的TableView中显示此模型:
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; }
我想将表列排序与列集成 Kundenwert。
显示的值应该是属性kundenwertFormatted(String),并且应该使用属性kundenwert(Double)进行排序。
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.
所以我写了比较器:
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());
但是当尝试按Kundenwert列排序时,我得到以下异常:
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)
指向此行:
kundenwertColumn.setComparator(new ProfilZuordnungTableRowModelComparator());
任何想法?
推荐答案
对于显示 Kundenwert
的列,使用 CellFactory
可以解决您的问题。这样,您不需要模型中的 kundenwertFormatted
字段,因为格式化将由单元格工厂完成:
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;
}
}
然后,您可以创建比较器来比较 kundenwert
这是双倍的:
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());
请注意,我使用了类型参数,因为 TableColumn
(和它的同伴)是通用的。
Note that I used type arguments since TableColumn
(and it's companions) is generic.
这篇关于使用Comparator进行Java FX表列排序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!