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:

    tableView: TableView[Note],
    tableViewContextMenu: ContextMenu,
    addNoteMenuItem: MenuItem,
    deleteNoteMenuItem: MenuItem,
) = {
    tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler[MouseEvent]() {
        override def handle(mouseEvent: MouseEvent): Unit = {
            if (mouseEvent.getButton == MouseButton.SECONDARY) {
      , 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.