I need to know how can I filter item(s) within ListView in JavaFX as I type letter in TextField. I tried to use FilteredList with help of different google's links but haven't got the result. To clarify, I'm posting a picture of feature I want. Thanks!
This feature I want in my ListView in JavaFX application
Your problem can be solved by two steps: set a FilteredList to your ListView, and bind a listener to your TextField.
//'myListView' is your ListView, 'myTextField' is your textfield
ObservableList<String> rawData= FXCollections.observableArrayList();
FilteredList<String> filteredList= new FilteredList<>(rawData, data -> true);
myListView.setItems(filteredList);
myTextField.textProperty().addListener(((observable, oldValue, newValue) -> {
filteredList.setPredicate(data -> {
if (newValue == null || newValue.isEmpty()){
return true;
}
String lowerCaseSearch=newValue.toLowerCase();
return String.valueOf(data.contains(lowerCaseSearch);
});
}));
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