java - 尝试从 @FXML TableView 中删除时出现 java.lang.UnsupportedOperationException
问题描述
我不明白为什么这种方法不起作用,因为它在三天前就起作用了。每当我尝试使用该方法(按下按钮)时,数据库操作都可以正常工作,但是每当我尝试从实际表视图中删除时程序都会抛出错误,这样用户就不会再看到该行了。我在初始化方法中添加了一个过滤列表,我担心这可能是问题的原因。这是我的代码:
初始化方法:
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
是使用创建的List<>
类型Collections.unmodifiableList()
。当您尝试修改其中一个时,UnsupportedOperationException
会抛出 an 。
public static List unmodifiableList(List<? extends T> list)
返回指定列表的不可修改视图。此方法允许模块为用户提供对内部列表的“只读”访问权限。对返回列表的查询操作“通读”到指定列表,并尝试修改返回的列表,无论是直接还是通过其迭代器,都会导致 UnsupportedOperationException。
推荐阅读
- python - 从服务器到网络驱动器的 Python 文件路径
- python - 预签名 URL 的下载文件而不是在浏览器中打开
- dynamic-loading - 如何动态加载依赖于主包中的模块的模块?
- c++ - 如何创建 bool BST::CloneSubtree(BST t1, type item) 函数以及如何调用 main ?我对 BST 很陌生。谢谢 ;)
- html - 如何知道站点是否在 Python 中启用了模式索引?
- elasticsearch - 在嵌套对象内查询和排序
- ms-access - 根据 MS Access 中重复条目的字段创建 ID
- javascript - 在这种情况下,Firestore 会将 oldEmail 添加到集合中?
- mysql - MYSQL连接两个表慢
- python - 如何检查多个行条目是否具有指示变量