【问题标题】:How can I right click on a cell in tableview in Javafx (fxml)?如何右键单击 Javafx (fxml) 中 tableview 中的单元格?
【发布时间】:2014-07-31 05:07:53
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: javafx tableview fxml


    【解决方案1】:

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

    参考standard table example 你可以做类似的事情

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

    【讨论】:

    • 感谢您的回答。博客确实很有帮助,但是如何识别右键呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多