rainer
rainer

Reputation: 3411

JavaFx add context menu from controller

I am trying my way through JavaFX and still have many - probably silly - beginner questions.

My problem of the day is the following:

I am creating, in Scene builder and Controller, a FlowPane to which I want to add a right-click option, that opens a Context Menu.

Through the scene builder I have added the function OnContextMenuRequested and defined it in the Controller.

To check, I have added a print commend and a Dialog Box to the function, which work well.

Yet, the Context Menu does not work..

Anybody could help and tell me what am I missing???

Thanks in advance...

public void contextMenu(ContextMenuEvent contextMenuEvent) {


    // working fine ..

    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setTitle("Information");
    alert.setHeaderText("Look");
    alert.setContentText("Message");
    alert.showAndWait();



    // working fine 

    System.out.println("Hello");


    // Context Menu ......... not working 

    ContextMenu contextMenu = new ContextMenu();
    MenuItem quit = new MenuItem("quit");
    MenuItem hello = new MenuItem("hello");
    contextMenu.getItems().addAll(quit, hello);
    contextMenu.setX(10.0);
    contextMenu.setY(10.0);
    contextMenu.show();

    ????.setContextMenu(????) 

    }

Upvotes: 0

Views: 1047

Answers (1)

fabian
fabian

Reputation: 82461

Unless you have a control, you need to show the ContextMenu "manually" using one of the methods defined in ContextMenu:

// contextMenu.setX(10.0);
// contextMenu.setY(10.0);

contextMenu.show((Node) contextMenuEvent.getSource(), contextMenuEvent.getScreenX(), contextMenuEvent.getScreenY());

Upvotes: 3

Related Questions