Fast filtering in javafx tableview

2019-03-28 09:13发布

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.

3条回答
时光不老,我们不散
2楼-- · 2019-03-28 09:18

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

查看更多
\"骚年 ilove
3楼-- · 2019-03-28 09:29

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)

查看更多
贼婆χ
4楼-- · 2019-03-28 09:38

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);
        });
查看更多
登录 后发表回答