Fast filtering in javafx tableview
21,378
Solution 1
You may use FilteredList
ObservableList<YourObjectClass> actualList = ...;
FilteredList<YourObjectClass> filteredList = new FilteredList<>(actualList);
TableView table = ...;
table.setItems(filteredList);
// to filter
filteredList.setPredicate(
new Predicate<YourObjectClass>(){
public boolean test(YourObjectClass t){
return false; // or true
}
}
);
as fast as swing, (maybe faster then swing... ). (I tested with 100000 rows)
Solution 2
You can use the following code. It works fine for me..
ObservableList data = table.getItems();
textfield.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
if (oldValue != null && (newValue.length() < oldValue.length())) {
table.setItems(data);
}
String value = newValue.toLowerCase();
ObservableList<T> subentries = FXCollections.observableArrayList();
long count = table.getColumns().stream().count();
for (int i = 0; i < table.getItems().size(); i++) {
for (int j = 0; j < count; j++) {
String entry = "" + table.getColumns().get(j).getCellData(i);
if (entry.toLowerCase().contains(value)) {
subentries.add(table.getItems().get(i));
break;
}
}
}
table.setItems(subentries);
});
Solution 3
@Override
public void initialize(URL arg0, ResourceBundle arg1)
{
searchField. textProperty().addListener((obs, oldText, newText) -> {
search();
});
@FXML private void search()
{
String keyword = searchField.getText();
if (keyword.equals("")) {
table.setItems(data);
} else {
ObservableList<Product> filteredData = FXCollections.observableArrayList();
for (Product product : data) {
if(product.getName().contains(keyword))
filteredData.add(product);
}
table.setItems(filteredData);
}
}
Solution 4
I use this snippet of code for filtering but really i didn't test in huge data case
textField.textProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
if(textField.textProperty().get().isEmpty()) {
tableView.setItems(dataList);
return;
}
ObservableList<ClassModel> tableItems = FXCollections.observableArrayList();
ObservableList<TableColumn<ClassModel, ?>> cols = tableView.getColumns();
for(int i=0; i<dataList.size(); i++) {
for(int j=0; j<cols.size(); j++) {
TableColumn col = cols.get(j);
String cellValue = col.getCellData(dataList.get(i)).toString();
cellValue = cellValue.toLowerCase();
if(cellValue.contains(textField.textProperty().get().toLowerCase())) {
tableItems.add(data.get(i));
break;
}
}
}
tableView.setItems(tableItems);
}
});
where ClassModel is your Model Class
Author by
learner
Updated on May 17, 2021Comments
-
learner about 3 years
I need to implement a filter in javafx tableview with huge data (around 100,000 ),
I have tried this tutorial. It works but filtering is really slow as compared to swing sorting and filtering, code.
Can anyone help me to increase speed.
What is happening right now is as I type textproperty change fire up and filterdata but it is slow, I need something which shows filter result with typing quickly as happening in swing.
thanks in advance.
p.s I have also looked at this.
-
bakr msaadi about 3 yearsYou may use that