如何从 TableView 中删除选中的行? (带有 FXML 的 JavaFX)
How to delete selected row from TableView? (JavaFX with FXML)
因此,我试图在我的程序中删除 TableView 中突出显示的行。
我在网上看了很多教程,但实在想不通。
我遵循了 Eclipse 中 CodeMakery 的示例,但我无法让它在 IntelliJ 上运行(因为一些明显的 JDK 问题?)
这是来自 CodeMakery 的代码:
private void handleDeletePerson() {
int selectedIndex = personTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
personTable.getItems().remove(selectedIndex);
} else {
// Nothing selected.
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("No Selection");
alert.setHeaderText("No Person Selected");
alert.setContentText("Please select a person in the table.");
alert.showAndWait();
}
}
能否请您帮助我了解如何删除所选行?
只需在该方法的开头添加personTable.setEditable(true)
。现在应该可以工作了。
因此,我试图在我的程序中删除 TableView 中突出显示的行。
我在网上看了很多教程,但实在想不通。 我遵循了 Eclipse 中 CodeMakery 的示例,但我无法让它在 IntelliJ 上运行(因为一些明显的 JDK 问题?)
这是来自 CodeMakery 的代码:
private void handleDeletePerson() {
int selectedIndex = personTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
personTable.getItems().remove(selectedIndex);
} else {
// Nothing selected.
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("No Selection");
alert.setHeaderText("No Person Selected");
alert.setContentText("Please select a person in the table.");
alert.showAndWait();
}
}
能否请您帮助我了解如何删除所选行?
只需在该方法的开头添加personTable.setEditable(true)
。现在应该可以工作了。