JavaFX 8 Tutorial 44 - Search or Filter Table By ID, FirstName and LastName
The code written in the tutorial is
TextField searchField;
searchField = new TextField();
searchField.setFont(Font.font("SanSerif", 15));
searchField.setPromptText("Search");
searchField.setMaxWidth(200);
fields.getChildren().addAll(searchField, label1, id, fn, ln, em, mobile, un, pw, date, male, female, checkBox1, checkBox2, checkBox3, button);
FilteredList<User> filteredData = new FilteredList<>(data, e -> true);
searchField.setOnKeyReleased(e ->{
searchField.textProperty().addListener((observableValue, oldValue, newValue) ->{
filteredData.setPredicate((Predicate<? super User>) user->{
if(newValue == null || newValue.isEmpty()){
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
if(user.getID().contains(newValue)){
return true;
}else if(user.getFirstName().toLowerCase().contains(lowerCaseFilter)){
return true;
}else if(user.getLastName().toLowerCase().contains(lowerCaseFilter)){
return true;
}
return false;
});
});
SortedList<User> sortedData = new SortedList<>(filteredData);
sortedData.comparatorProperty().bind(table.comparatorProperty());
table.setItems(sortedData);
});
Comments
Post a Comment