作者:七楼居民_651 | 来源:互联网 | 2023-05-22 15:08
我的应用程序中有一个包含字符串的ListView.现在,我想通过输入字段过滤此列表.但是我发现的有关FilteredLists的所有示例都是关于如何过滤表格的.
如何使用FilteredList过滤ListView?
1> eckig..:
这很简单明了:
@Override
public void start(Stage primaryStage) {
ObservableList data = FXCollections.observableArrayList();
IntStream.range(0, 1000).mapToObj(Integer::toString).forEach(data::add);
FilteredList filteredData = new FilteredList<>(data, s -> true);
TextField filterInput = new TextField();
filterInput.textProperty().addListener(obs->{
String filter = filterInput.getText();
if(filter == null || filter.length() == 0) {
filteredData.setPredicate(s -> true);
}
else {
filteredData.setPredicate(s -> s.contains(filter));
}
});
BorderPane cOntent= new BorderPane(new ListView<>(filteredData));
content.setBottom(filterInput);
Scene scene = new Scene(content, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}