JavaFX: How to add a ContextMenu to a TableView

As a note to self, I used code like this in a Scala + JavaFX application to add a ContextMenu to a TableView:

defaddContextMenuToTableView(
    tableView: TableView[Note],
    tableViewContextMenu: ContextMenu,
    addNoteMenuItem: MenuItem,
    deleteNoteMenuItem: MenuItem,
) = {
    tableViewContextMenu.getItems().add(addNoteMenuItem)
    tableViewContextMenu.getItems().add(deleteNoteMenuItem)
    tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler[MouseEvent]() {
        override def handle(mouseEvent: MouseEvent): Unit = {
            if (mouseEvent.getButton == MouseButton.SECONDARY) {
                tableViewContextMenu.show(tableView, mouseEvent.getScreenX, mouseEvent.getScreenY)
            }
        }
    })
}

There wasn’t anything special about the buttons, they were regular JavaFX Buttons:

val addNoteButton = new Button("add note")
val deleteNoteButton = new Button("delete note")

I did this in my Notes application, but later decided not to use a popup menu for this purpose.