How to delete a JavaFx TableView Row


Question

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 database via Hibernate.

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.

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"));
}
1
4
3/22/2014 6:25:06 AM

Accepted Answer

I have created a SSCCE to help with deletion of row data with a button. Please have a look at the following code :

TableViewDeleteSample

16
3/24/2014 7:44:09 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon