java.lang.UnsupportedOperationException,用于从javafx tableview中删除一行 [英] java.lang.UnsupportedOperationException for removing a row from the javafx tableview
本文介绍了java.lang.UnsupportedOperationException,用于从javafx tableview中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从javafx的tableview中删除选定的记录. 下面是我用数据填充表的方式:
I am trying to delete the selected record from the tableview in javafx. Below is how I am populating the table with the data:
public void setMainApp(MainAppClass mainApp){
this.mainApp = mainApp;
FilteredList<FileModel> filteredData = new FilteredList<>(mainApp.getFileData(), p -> true);
// 2. Set the filter Predicate whenever the filter changes.
filterField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(files -> {
// If filter text is empty, display all files.
if (newValue == null || newValue.isEmpty()) {
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
if (files.getFileSubject().toLowerCase().indexOf(lowerCaseFilter) != -1) {
return true; // Filter matches Subject.
}
else if (files.getFileDate().toLowerCase().indexOf(lowerCaseFilter) != -1) {
return true; // Filter matches last name.
}
return false; // Does not match.
});
});
// 3. Wrap the FilteredList in a SortedList.
SortedList<FileModel> sortedData = new SortedList<>(filteredData);
// 4. Bind the SortedList comparator to the TableView comparator.
sortedData.comparatorProperty().bind(fileTable.comparatorProperty());
// 5. Add sorted (and filtered) data to the table.
fileTable.setItems(sortedData);
}
这就是我要删除记录的方式:
And thats how I am removing the record:
@FXML
private void deleteFile() {
int selectedIndex = fileTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
fileTable.getItems().remove(selectedIndex);
} else {
// Nothing selected.
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("No Selection");
alert.showAndWait();
}
}
但是它给出了java.lang.UnsupportedOperationException
错误.我在示例项目中做了同样的事情,一切顺利.那么,我该如何解决这个问题?
But it gives java.lang.UnsupportedOperationException
error. I have done the same thing in my sample project and it goes fine. So, how can I resolve this issue?
推荐答案
从基础列表中删除数据,而不是从过滤/排序后的列表中删除数据:
Remove the data from the underlying list, not the filtered/sorted list:
FileModel selectedItem = fileTable.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
mainApp.getFileData().remove(selectedItem);
}
这篇关于java.lang.UnsupportedOperationException,用于从javafx tableview中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文