根据http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/中的示例,我有一个构建TableView的FilteredList。
默认谓词工作正常,当PropertyChangeListener触发时,每次都会正确调用setPredicate()方法,但只有FIRST调用会导致运行谓词过滤器代码并随后更新TableView。
我在这里错过了什么?
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
.
.
.
// Wrap the ObservableList in a FilteredList (initially display all data).
FilteredList<Filedata> filteredData = new FilteredList<>( eeModel.data, p -> true );
// 2. Set the filter Predicate whenever the filter changes.
FilterDataClass.addPropertyChangeListener( new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("PCE...");
filteredData.setPredicate(fd -> {
if ( FilterDataClass.getFilenameDupe() == FilterDataClass.FilterStatus.cbIGNORE ) {
return true;
}
else if ( (FilterDataClass.getFilenameDupe() == FilterDataClass.FilterStatus.cbON) && fd.isFilenameDuplicate() ) {
return true;
}
else if ( (FilterDataClass.getFilenameDupe() == FilterDataClass.FilterStatus.cbOFF) && !fd.isFilenameDuplicate() ) {
return true;
}
else {
return false;
}
});
}
});
// Wrap the FilteredList in a SortedList.
SortedList<Filedata> sortedList = new SortedList<>( filteredData );
// create a TableView with the sorted list set as the items it will show
table.setItems( sortedList );
// bind the sortedList comparator to the TableView comparator
sortedList.comparatorProperty().bind(table.comparatorProperty());
}
编辑:
这可能是一个优化问题吗?如果我将一些垃圾代码添加到需要评估事件数据的谓词中,例如:
if ( evt.getNewValue() == evt.getOldValue() ) {
System.out.println("This should never happen");
}
然后重新评估谓词。