简体   繁体   English

如何删除JavaFx TableView行

[英]How to delete a JavaFx TableView Row

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 . 我有一个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. 按钮单击以删除它所属的TableRow以及填充该特定TableRow的数据库项。 So far nothing happens at all on ButtonClick. 到目前为止,ButtonClick上根本没有任何事情发生。

Thank you in advance. 先感谢您。

Ps. 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"));
}

I have created a SSCCE to help with deletion of row data with a button. 我创建了一个SSCCE来帮助删除行数据。 Please have a look at the following code : 请看下面的代码:

TableViewDeleteSample TableViewDeleteSample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM