How can I right click on a cell in tableview in 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?


6/10/2014 12:26:40 PM

Accepted Answer

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>>() {
    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>() {
            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 ;
4/27/2018 4:55:43 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow