質問

How can I add some action to a JavaFx Button in a TableView Row so that that particular TableRow and the corresponding database table that populates the particular Row gets deleted when the button is pressed?

Each Row on the TableView will have it's own delete Button.

This what I have so far:

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"));

Any help will be greatly appreciated. Been at this for the last two days.

役に立ちましたか?

解決

The most obvious way to do it is to remove the item from the ObservableList or clear the ObservableList and repopulate it, then assign this list to the table again !

This will work out !

-Cheers !

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top