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.
How can I filter a ListView using a FilteredList?
That is rather simple and straightforward:
@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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With