JavaFx FilteredList .setPredicate 不起作用

标签 java javafx-8

<分区>

我正在尝试通过与此 example 相同的 JavaFx 中的文本字段对 TableView 进行筛选。

不幸的是,我没有得到任何真实的返回。 FilteredList.setPredicate 似乎未运行或在 Debug模式下被跳过。

public class App extends Application {

TableView tableScanSystemForSnapshots = new TableView<>(); 
private ObservableList<Snapshot> snapshots = FXCollections.observableArrayList();
private FilteredList<Snapshot> filteredListSnapshots = new FilteredList<>(snapshots, s -> true);

public App() {

}

@Override
public void start(Stage primaryStage) throws Exception {
    //Table View Snapshots
    tableScanSystemForSnapshots.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); //make multiple selection in TableView possible

    //Initialize Columns
    TableColumn<Snapshot, String> colVmName = new TableColumn<>("VM Name");
    TableColumn<Snapshot, String> colSnapshotName = new TableColumn<>("Snapshot Name");
    TableColumn<Snapshot, String> colSnapshotDescription = new TableColumn<>("Description");
    TableColumn<Snapshot, String> colCreatedTime = new TableColumn<>("created Time");
    TableColumn<Snapshot, String> colCreatedBy = new TableColumn<>("created by");

    //Initialize ValueFactory - name has to be the same as in Class: Snapshot
    colVmName.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("vmName"));
    colSnapshotName.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("snapshotName"));
    colSnapshotDescription.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("description"));
    colCreatedTime.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("createdTime"));
    colCreatedBy.setCellValueFactory(new PropertyValueFactory<Snapshot, String>("createdBy"));

    //TextField Filter
    TextField txtFilter = new TextField();
    txtFilter.textProperty().addListener(((observable, oldValue, newValue) -> {
        filteredListSnapshots.setPredicate(s -> {
            if(newValue == null||newValue.isEmpty()){
                return true;
            }
            //Compare
            String lowerCaseFilter = newValue.toLowerCase();

            if (s.getVmName().toLowerCase().contains(lowerCaseFilter)){return true;} //Filter matches VM Name
            if (s.getSnapshotName().toLowerCase().contains(lowerCaseFilter)){return true;} //Filter matches Snapshot Name
            return false; // Does not match
        });

        SortedList<Snapshot> sortedData = new SortedList<Snapshot>(filteredListSnapshots);
        sortedData.comparatorProperty().bind(tableScanSystemForSnapshots.comparatorProperty());
        tableScanSystemForSnapshots.setItems(sortedData);
    }));

最佳答案

ObservableList 快照未正确填充,更正后,它可以正常工作。

关于JavaFx FilteredList .setPredicate 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40044569/

相关文章:

java - 如何在java中打印0,1,2的字典排列

java - 为什么 multi-catch RuntimeException 可以编译,而 multi-catch Exception 不编译?

java - 我不知道如何使用类类型作为参数

java - 如何更新JAVAFX TableView 中每一行的值

Java - 基于多个分隔符拆分字符串

java - mysql 主机服务器未连接

JavaFx TreeTableCell 宽度计算

junit - JavaFX 8的基本JUnit测试

JavaFX 矩形转圆形动画

命令提示符中的 javafx 参数给出空值