JavaFX 8:TableView过滤&行删除

时间:2017-01-09 07:05:47

标签: model-view-controller javafx tableview javafx-8

请原谅下面的代码块,我为了清楚起见而提出这些代码。谢谢!

我有一个类型为[TestSuiteDetail]的表视图控件[suiteCasesSelected],它具有以下功能:

  • 我从本教程获得的过滤功能:JavaFX 8 TableView Sorting and Filtering

    //Triggered every time I type something on the filter box
    private void filterTestSuiteSelectedList() {
        // Wrap the ObservableList in a FilteredList (display all data).
        FilteredList<TestSuiteDetail> filteredData = new FilteredList<>(mainApp.getTestSuiteSelectedData(), p -> true);
    
        // Set the filter Predicate whenever the filter changes.
        suiteSelectedFilter.textProperty().addListener((observable, oldValue, newValue) -> {
            filteredData.setPredicate(tc -> {
                // Display all if filter is empty
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }
                // Compare test case name or module name
                String lowerCaseFilter = newValue.toLowerCase();
                if (tc.getTestCaseName().toLowerCase().contains(lowerCaseFilter)) {
                    return true;
                } else if (tc.getModuleName().toLowerCase().contains(lowerCaseFilter)) {
                    return true;
                }
                return false;
            });
        });
    
        // Wrap the FilteredList in a SortedList.
        SortedList<TestSuiteDetail> sortedData = new SortedList<>(filteredData);
    
        // Bind the SortedList comparator to the TableView comparator.
        sortedData.comparatorProperty().bind(suiteCasesSelected.comparatorProperty());
    
        // Add sorted and filtered data to the table.
        suiteCasesSelected.setItems(sortedData);
    }
    

  • 通过按下按钮从列表中删除项目:

    @FXML
    private void handleRemoveFromSelectedClicked() {
        ObservableList selectedItems = suiteCasesSelected.getSelectionModel().getSelectedItems();
        if (selectedItems != null) {
            suiteCasesSelected.getItems().removeAll(selectedItems);
            mainApp.saveSuiteDetailsSelectedXMLData(testSuite);
            TestNGXmlConverter.updateTestSuiteXMLFile(testSuite.getTestSuiteName());
        }
    }
    

    我的问题:
    每次我从表格中选择项目,然后点击删除&#39;&lt;&lt;&lt;&#39;按钮,我从

    开始有多个例外
     Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    

    最后以

    结束
     Caused by: java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(Unknown Source)
    at java.util.AbstractList$Itr.remove(Unknown Source)
    at java.util.AbstractCollection.removeAll(Unknown Source)
    at cl.oe.beatrix.view.ControllerWindowMain.handleRemoveFromSelectedClicked(ControllerWindowMain.java:2043)
    ... 70 more
    

    第2043行指向上面显示的suiteCasesSelected.getItems().removeAll(selectedItems);

    其他信息:

  • 我在控制器类的filterTestSuiteSelectedList()方法上调用了整个过滤方法initialize()
  • 只有在我放入过滤功能时才会发生异常。如果我发表评论,它的工作正常。

    感谢您的帮助!如果需要进一步的信息,请告诉我们!

    Table snapshot

  • 2 个答案:

    答案 0 :(得分:1)

    管理解决这个问题。

    用户遇到类似问题并将其发布在教程See the comments all the way below the blog post中。

    原材料:

    // The index of the sorted and filtered list.
    int visibleIndex = personTable.getSelectionModel().getSelectedIndex();
    
    // Source index of master data.
    int sourceIndex = sortedData.getSourceIndexFor(masterData, visibleIndex);
    
    // Remove.
    masterData.remove(sourceIndex);
    

    根据我的情况修改(多选表):

    @FXML
    private void handleRemoveFromSelectedClicked() {
        // Get all the selected items
        ObservableList<Integer> selectedItems = suiteCasesSelected.getSelectionModel().getSelectedIndices();
        if (selectedItems != null) {
            // Iterate through each item
            for (Integer selectedItem : selectedItems) {
                int sourceIndex = filteredSelectedData.getSourceIndexFor(mainApp.getTestSuiteSelectedData(),
                        selectedItem.intValue());
                //remove        
                mainApp.getTestSuiteSelectedData().remove(sourceIndex);
            }
    
            mainApp.saveSuiteDetailsSelectedXMLData(testSuite);
            TestNGXmlConverter.updateTestSuiteXMLFile(testSuite.getTestSuiteName());
        }
    }
    

    谢谢!

    答案 1 :(得分:1)

    由于external jarFilteredList只是列表的视图,因此它们不可修改;只能通过修改支持列表来删除项目:

    SortedList

    BTW:您不需要检查ObservableList<TestSuiteDetail> modifiableSource = ... FilteredList<TestSuiteDetail> filteredList = new FilteredList<(modifiableSource); SortedList<TestSuiteDetail> sortedList = new SortedList<>(filteredList); ... modifiableSource.removeAll(selectedItems); suiteCasesSelected.getSelectionModel().getSelectedItems()的结果,因为这总是会返回null,但{{1}可能是空的......