Using FXML to Create ContextMenu within a Pane

元气小坏坏 提交于 2019-12-04 05:52:42

Tried to do it within a Popup? Catch the MouseEvent check for MouseButton.SECONDARY and display your VBox in the Popup.

private void showContextMenu() // showContextMenu(MouseEvent event)
{
    Popup popup = new Popup();

    VBox content = new VBox();
    Button b = new Button("Click Me!");
    b.setOnAction(new EventHandler<ActionEvent>() { });
    content.getChildren().addAll(b);

    popup.getContent().add(content);
    popup.setX(0); // or get mouse event x and y
    popup.setY(0); // event.getY()
    popup.setAutoHide(true);
    popup.show(your popup owner);
}

In This example there is button which has context menu when click on left/right it will open the popup when you click on other area except button it will hide the popup context menu

public class Main extends Application{

public static void main(String[] args) {
    launch(args);
  }
  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);
    Button notification = new Button();

    MenuItem item1 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("About");
      }
    });
    MenuItem item2 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("Preferences");
      }
    });

    MenuItem item3 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("About");
      }
    });
    MenuItem item4 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("Preferences");
      }
    });
    MenuItem item5 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("About");
      }
    });
    MenuItem item6 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("Preferences");
      }
    });

    MenuItem item7 = new MenuItem("About");
    item1.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("About");
      }
    });
    MenuItem item8 = new MenuItem("Preferences");
    item2.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent e) {
        System.out.println("Preferences");
      }
    });
    final ContextMenu contextMenu = new ContextMenu(item1, item2,item3, item4,item5, item6,item7, item8);
    contextMenu.setMaxSize(50, 50);

    contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
      public void handle(WindowEvent e) {
        System.out.println("showing");
      }
    });
    contextMenu.setOnShown(new EventHandler<WindowEvent>() {
      public void handle(WindowEvent e) {
        System.out.println("shown");
      }
    });

   // contextMenu.hide();


    notification.setContextMenu(contextMenu);
    GridPane grid = new GridPane();
    grid.setVgap(4);
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(new Label("To: "), 0, 0);
    grid.add(notification, 1, 0);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();
    notification.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me)->{
        if(me.getButton()==MouseButton.PRIMARY ){
            System.out.println("Mouse Left Pressed");
            System.out.println(notification.getScaleX());
            System.out.println(notification.getScaleY());
            System.out.println(me.getScreenX());
            System.out.println(me.getScreenY());
            contextMenu.show(notification,me.getScreenX(),me.getScreenY());
        }else{
            contextMenu.hide();
        }
    });
  }

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!