Fast filtering in javafx tableview

I need to implement a filter in the form of a javafx table with huge data (about 100,000),

I tried this tutorial . It works, but filtering is very slow compared to swing sorting and filtering code .

Can anyone help me increase the speed.

What is happening right now, since I'm typing textproperty change fire and filterdata, but it's slow, I need something that shows the result of the filter, quickly typing how it happens in swing.

early.

ps I also reviewed this one .

+6
source share
3 answers

You can 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 rocking (maybe faster than rocking ...). (I tested 100,000 rows)

+4
source

You can use the following code. This works great 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); }); 
+3
source

I use this code snippet for filtering, but in fact I have not tested in a 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

+1
source

Source: https://habr.com/ru/post/1485361/


All Articles