java javafx tableview中的快速过滤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17017364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:43:41  来源:igfitidea点击:

Fast filtering in javafx tableview

javaswingjavafx-2tableview

提问by learner

I need to implement a filter in javafx tableview with huge data (around 100,000 ),

我需要在具有大量数据(大约 100,000 )的 javafx tableview 中实现过滤器,

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.

现在发生的事情是,当我键入 textproperty 更改启动和 filterdata 但它很慢时,我需要一些显示过滤结果的东西,如在 Swing 中发生的那样快速键入。

thanks in advance.

提前致谢。

p.s I have also looked at this.

ps 我也看过这个

回答by guleryuz

You may use FilteredList

您可以使用 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)

像摆动一样快,(也许比摆动更快......)。(我测试了 100000 行)

回答by Harshita Sethi

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);
        });

回答by Zaid Sultan

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

其中 ClassModel 是您的模型类