如何右键单击Javafx(fxml)中tableview中的单元格? [英] How can I right click on a cell in tableview in Javafx (fxml)?

查看:256
本文介绍了如何右键单击Javafx(fxml)中tableview中的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaFX和fxml编写表视图。我想在用户右键单击表格中的单元格时执行某些操作。我怎样才能做到这一点?
是否可以在单元格上创建右键菜单?

I am programming a table view with JavaFX and fxml. I want to do some actions when a user right clicks on a cell in the table. How can I do that? Is it possible to create a right click menu over a cell?

谢谢!

推荐答案

为感兴趣的表列实现单元工厂。在单元工厂中创建一个单元格,并在单元格中注册鼠标侦听器。

Implement a cell factory for the table column(s) of interest. Create a cell in the cell factory and register the mouse listener with the cell.

参考标准表示例你可以做类似的事情

Referring to the standard table example you can do something like

firstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
    @Override
    public TableCell<Person, String> call(TableColumn<Person, String> col) {
        final TableCell<Person, String> cell = new TableCell<>();
        cell.textProperty().bind(cell.itemProperty()); // in general might need to subclass TableCell and override updateItem(...) here
        cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getButton == MouseButton.SECONDARY) {
                    // handle right click on cell...
                    // access cell data with cell.getItem();
                    // access row data with (Person)cell.getTableRow().getItem();
                }
            }
        });
        return cell ;
    }
});

这篇关于如何右键单击Javafx(fxml)中tableview中的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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