Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast filtering in javafx tableview

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.

like image 790
learner Avatar asked Jun 10 '13 05:06

learner


People also ask

How to filter TableView in JavaFX?

We can filter TableView content in two main ways – manually, or by using the FilteredList class JavaFX provides. In either case, we can update our search criteria by placing a ChangeListener on the search box TextField. This way, each time the user changes their search, the TableView is updated automatically.


2 Answers

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)

like image 191
guleryuz Avatar answered Oct 21 '22 14:10

guleryuz


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);
        });
like image 21
Harshita Sethi Avatar answered Oct 21 '22 12:10

Harshita Sethi