<分区>
我正在尝试通过与此 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);
}));