如何删除 JavaFx TableView 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22574239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a JavaFx TableView Row
提问by ILikeProgramming
I have a JavaFx TableView with each Row having a column with a delete button which when clicked should delete the TableRow, as well as the corresponding entries in the H2 databasevia Hibernate.
我有一个 JavaFx TableView,每一行都有一个带有删除按钮的列,单击该按钮时应该删除 TableRow,以及通过 Hibernate删除H2 数据库中的相应条目。
So far I'm not getting anything. Nothing happens on button click. Not even if I manually assign the item Primary Key like so:
到目前为止,我什么也没得到。单击按钮时没有任何反应。即使我像这样手动分配项目主键也不会:
NewBeautifulKiwi toDelete = (NewBeautifulKiwi) session.get(NewBeautifulKiwi.class, 97);
Please help me make this work; the button click to delete the TableRow it belongs to as well as the Database items populating that particular TableRow. So far nothing happens at all on ButtonClick.
请帮我完成这项工作;单击该按钮可删除它所属的 TableRow 以及填充该特定 TableRow 的数据库项。到目前为止,ButtonClick 上什么也没有发生。
Thank you in advance.
先感谢您。
Ps.
附言。
The buttons also get printed where the columns are empty. It would also help if you helped me solve this and only have Buttons on Rows with data
在列为空的地方也会打印按钮。如果您帮我解决这个问题并且只有带有数据的行上的按钮,这也会有所帮助
The Class Extract:
类摘录:
public class HomeController implements Initializable {
@FXML
public static TableView<NewBeautifulKiwi> KIWI_TABLE;
@FXML
private TableColumn<NewBeautifulKiwi, Object> KiwiAction;
// Initializes the controller class.
@Override
public void initialize(URL url, ResourceBundle rb) {
KiwiAction.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, Object>("KiwiAction"));
KiwiAction.setCellFactory(new Callback<TableColumn<NewBeautifulKiwi, Object>, TableCell<NewBeautifulKiwi, Object>>() {
@Override
public TableCell<NewBeautifulKiwi, Object> call(TableColumn<NewBeautifulKiwi, Object> param) {
final Button button;
Image image = new Image(getClass().getResourceAsStream("/MediaTools/Error.png"));
final ImageView imageView = new ImageView();
imageView.setFitHeight(16);
imageView.setFitWidth(16);
imageView.setImage(image);
button = new Button("", imageView);
final TableCell<NewBeautifulKiwi, Object> cell = new TableCell<NewBeautifulKiwi, Object>() {
@Override
public void updateItem(Object item, boolean empty) {
if (item != null) {
super.updateItem(item, empty);
final VBox vbox = new VBox(0);
button.setAlignment(Pos.CENTER);
button.maxWidth(32);
button.getStyleClass().add("deleteButton");
final TableCell<NewBeautifulKiwi, Object> c = this;
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
TableRow tableRow = c.getTableRow();
NewBeautifulKiwi item = (NewBeautifulKiwi) tableRow.getTableView().getItems().get(tableRow.getIndex());
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
NewBeautifulKiwi toDelete = (NewBeautifulKiwi) session.get(NewBeautifulKiwi.class, item);
session.delete(toDelete);
session.getTransaction().commit();
session.flush();
session.close();
System.out.println("Deleted");
}
});
vbox.getChildren().add(button);
setGraphic(vbox);
}
}
};
cell.setGraphic(button);
return cell;
}
});
});
Kiwi.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, String>("Kiwi"));
}