簡體   English   中英

嘗試從@FXML TableView 中刪除時出現 java.lang.UnsupportedOperationException

[英]java.lang.UnsupportedOperationException when trying to delete from an @FXML TableView

我不明白為什么這種方法不起作用,因為它在三天前就起作用了。 每當我嘗試使用該方法(按下按鈕)時,數據庫操作都可以正常工作,但是每當我嘗試從實際表視圖中刪除時程序都會拋出錯誤,這樣用戶就不會再看到該行了。 我在初始化方法中添加了一個過濾列表,我擔心這可能是問題的原因。 這是我的代碼:

初始化方法:

    private void initialize()
    {
        ObservableList<BloomClient> clients = FXCollections.observableArrayList();
        firstNames.setCellValueFactory(new PropertyValueFactory<>("FirstName"));
        lastNames.setCellValueFactory(new PropertyValueFactory<>("LastName"));
        phoneNumbers.setCellValueFactory(new PropertyValueFactory<>("PhoneNumber"));
        birthdays.setCellValueFactory(new PropertyValueFactory<>("Birthday"));
        startDates.setCellValueFactory(new PropertyValueFactory<>("StartDate"));
        endDates.setCellValueFactory(new PropertyValueFactory<>("ExpireDate"));
        try {
            clients = dBconnect.getClientList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        FilteredList<BloomClient> filteredList = new FilteredList<BloomClient>(clients,b -> true);
        filteredSearch.textProperty().addListener(((observable, oldValue, newValue) ->
                filteredList.setPredicate(person ->
                {
                    if(newValue == null || newValue.isEmpty())
                        return true;//nothing in text field
                    String lowerCaseFilter = newValue.toLowerCase();
                    if (person.getFirstName().toLowerCase().contains(lowerCaseFilter))
                        return true;//check first name
                    else if (person.getLastName().toLowerCase().contains(lowerCaseFilter))
                        return true;//check last name
                    else
                        return false;
                })
                ));
        SortedList<BloomClient> sortedList = new SortedList<>(filteredList);
        sortedList.comparatorProperty().bind(clientList.comparatorProperty());
        clientList.setItems(sortedList);
    }



public void freezeAccount() throws SQLException, ParseException {
        BloomClient person = clientList.getSelectionModel().getSelectedItem();
        dBconnect.sendToFreeze(person);//this works
        dBconnect.deleteClient(person);//this works
        clientList.getItems().remove(person);//java.lang.UnsupportedOperationException
        clientList.refresh();
    }

好吧,這只是一個猜測。 但是可能clientList是使用Collections.unmodifiableList()創建的List<>類型。 當您嘗試修改其中之一時,會引發UnsupportedOperationException

public static List unmodifiableList(List<? extends T> list)

返回指定列表的不可修改視圖。 此方法允許模塊為用戶提供對內部列表的“只讀”訪問權限。 對返回列表的查詢操作“通讀”到指定列表,並嘗試修改返回的列表,無論是直接還是通過其迭代器,都會導致 UnsupportedOperationException。

請參閱: https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-

以某種方式想通了,但想要更深入的解釋。 我最終沒有從 clientList(TableView) 中刪除,而是直接從 clients(ObservableList) 中刪除,並使該變量成為全局變量,以供其他方法訪問。 我不確定最初問題背后的原因。

        BloomClient person = clientList.getSelectionModel().getSelectedItem();
        dBconnect.deleteClient(person);
        clients.remove(person);
        clientList.refresh();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM