如何在TableView Row中向JavaFx Button添加一些操作,以便在按下按钮时删除特定的TableRow和填充特定Row的相应数据库表?
TableView上的每一行都有自己的删除按钮。
这是我到目前为止所做的:
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"));
任何帮助将不胜感激。过去两天一直在这。
答案 0 :(得分:2)
最明显的方法是从ObservableList
中删除该项目或清除ObservableList
并重新填充该项目,然后再将此列表分配给该表格!
这样就可以了!
-Cheers!