如何在 ListView 中使用 JavaFX FilteredList? [英] How to use JavaFX FilteredList in a ListView?
本文介绍了如何在 ListView 中使用 JavaFX FilteredList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序中有一个包含字符串的 ListView.现在,我想通过输入字段过滤此列表.但是我发现的所有关于 FilteredLists 的例子都是关于如何过滤表格的.
I have a ListView in my application which contains Strings. Now, I want to filter this list by an input field. But all examples about FilteredLists I found are about how to filter a table.
如何使用 FilteredList 过滤 ListView?
How can I filter a ListView using a FilteredList?
推荐答案
那相当简单明了:
@Override
public void start(Stage primaryStage) {
ObservableList<String> data = FXCollections.observableArrayList();
IntStream.range(0, 1000).mapToObj(Integer::toString).forEach(data::add);
FilteredList<String> 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();
}
这篇关于如何在 ListView 中使用 JavaFX FilteredList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文