如何删除JavaFx TableView Row [英] How to delete a JavaFx TableView Row

查看:457
本文介绍了如何删除JavaFx TableView Row的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



到目前为止,我没有收到任何东西。按钮点击没有任何反应。即使我手动分配项目主键像这样:

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

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



预先感谢您。

Ps。



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



类提取: p>

  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 =新的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 ,布尔空){
if(it em!= 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 =这个;

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(已删除);
}
});
vbox.getChildren()。add(button);
setGraphic(vbox);
}

}
};
cell.setGraphic(button);
返回单元格;
}
});

});

Kiwi.setCellValueFactory(new PropertyValueFactory< NewBeautifulKiwi,String>(Kiwi));
}


解决方案

href =http://www.sscce.org> SSCCE 帮助用按钮删除行数据。请看下面的代码:



TableViewDeleteSample


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

解决方案

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

TableViewDeleteSample

这篇关于如何删除JavaFx TableView Row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆