如何删除JavaFx TableView行

时间:2014-03-22 06:25:07

标签: java hibernate javafx tableview

我有一个JavaFx TableView,每一行都有一个带删除按钮的列,点击该按钮时删除TableRow 以及H2数据库中的相应条目 通过Hibernate

到目前为止,我没有得到任何东西。按钮点击没有任何反应。即使我手动分配项主键,如下所示:

NewBeautifulKiwi toDelete = (NewBeautifulKiwi) session.get(NewBeautifulKiwi.class, 97);

请帮我做这项工作;按钮单击以删除它所属的TableRow以及填充该特定TableRow的数据库项。到目前为止,ButtonClick上根本没有任何事情发生。

提前谢谢。

聚苯乙烯。

按钮也会打印在列为空的位置。如果你帮助我解决了这个问题,并且只有行数据上的按钮

,那也会有所帮助

课程提取:

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 个答案:

答案 0 :(得分:16)

我创建了一个SSCCE来帮助您使用按钮删除行数据。请查看以下代码:

TableViewDeleteSample