I'm using a TextField with a listener to filter a TableView. I've got if statements that set the predicate if newText matches part of the ID or Name.
The search functionality works like I want.
However, my project guidelines require me to also create some form of output upon a search not matching any objects in the TableView.
I created a Label and set the visibility to false to start. I placed the code to set the Label to visible after each of the if statements in the predicate logic so that it only shows if none of the statements are valid. I then placed a line setting it back to false at the beginning of the predicate logic so on each listener change it gets reset.
I am having an issue where if my search returns the last item in the list it is fine and the label doesn't appear, but if the search doesn't return the last item in the list then the label becomes visible even though the search displays items from the sorted list.
The following is my code and I've attached screenshots of the "bug".
FilteredList<Part> filteredParts = new FilteredList<>(Inventory.getAllParts(), p -> true);
partFilter.textProperty().addListener((ob, ol, newValue) -> {
filteredParts.setPredicate(text -> {
//each time listener is updated sets label visibility to false
partSearchMismatch.setVisible(false);
//If searchbar is empty or null it displays all parts
if (newValue == null || newValue.isEmpty()){
return true;
}
//if text matches partID
String lowerCaseFilter = newValue.toLowerCase();
if(String.valueOf(text.getId()).contains(lowerCaseFilter)){
return true;
}
//if text matches part name
if(text.getName().toLowerCase().contains(lowerCaseFilter)){
return true;
}
partSearchMismatch.setVisible(true); //if no matches, displays label
return false; //no match
});
});
//Wraps filtered list in sorted list
SortedList<Part> sortedParts = new SortedList<>(filteredParts);
//Binds sorted list comparator to the parts table comparator
sortedParts.comparatorProperty().bind(partsTable.comparatorProperty());
partsTable.setItems(sortedParts);
The following is the base sample data in table
Image of search that matches first object in list but label showing anyways
Image of search that matches second object in list but label showing anyways
Image of label working as long as last object in table is included in search



