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?
Thanks!
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?
Thanks!
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 ;
}
});