I have a JavaView TableView, each of which has a column with a delete button, which when clicked should delete the TableRow, as well as the corresponding entries in the H2 database via Hibernate .
So far I havenβt received anything. Nothing happens when a button is pressed. Even if I manually assigned the Primary Key element as follows:
NewBeautifulKiwi toDelete = (NewBeautifulKiwi) session.get(NewBeautifulKiwi.class, 97);
Please help me do this work; click the button to remove the TableRow that it belongs to, as well as the database items that populate this particular TableRow. So far, nothing is happening on ButtonClick.
Thanks in advance.
Ps.
Buttons also print when the columns are empty. It would also help if you could help me solve this problem and only have buttons on the data lines
Class extraction:
public class HomeController implements Initializable {
@FXML
public static TableView<NewBeautifulKiwi> KIWI_TABLE;
@FXML
private TableColumn<NewBeautifulKiwi, Object> KiwiAction;
@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"));
}