Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter item(s) within ListView in JavaFX

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

like image 534
Asad Ullah Khalid Avatar asked Oct 26 '25 03:10

Asad Ullah Khalid


1 Answers

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);
        });
    }));
like image 135
Xu Lei Avatar answered Oct 28 '25 18:10

Xu Lei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!