1 Reply Latest reply: Oct 22, 2012 2:08 PM by shakir.gusaroff RSS

    Can't listen to mouseclick or action in Menu?

    968989
      I have a Menu inside a MenuBar without MenuItems, and I'm unable to set any Action for it.. I have tried these approaches with no success
      mnu.setOnAction
      mnu.addEventHandler(MouseEvent
      Can't use
      mnu.showingProperty().addListener(new ChangeListener 
      as this Menu has no MenuItem


      Any ideas?
        • 1. Re: Can't listen to mouseclick or action in Menu?
          shakir.gusaroff
          Hi. The menu control is working as expected.
          If the menu does not contain menu items you can use Menu.setGraphic(Node node);
           public void start(Stage primaryStage) {
          
                  MenuBar menuBar = new MenuBar();
                     
                  Text hp = new Text("File");
                  hp.setFill(Color.WHITE);
                  Menu menu1 = new Menu();
                  menu1.setGraphic(hp);
          
                  hp.setOnMouseClicked(new EventHandler<MouseEvent>() {
                      @Override
                      public void handle(MouseEvent event) {
                          System.out.println("Menu clicked");
                      }
                  });
          
                  final Menu menu2 = new Menu("Options");
                  final Menu menu3 = new Menu("Help");
                  menuBar.getMenus().addAll(menu1, menu2, menu3);
          
                  StackPane root = new StackPane();
                  root.getChildren().add(menuBar);
          
                  Scene scene = new Scene(root, 300, 250);
          
                  primaryStage.setScene(scene);
                  primaryStage.show();
              }
          Or You can use a Toolbar instead of a Menubar:
                  ToolBar toolBar = new ToolBar() ;
                  final Button menu1 = new Button("File");
                  final Button menu2 = new Button("Options");
                  final Button menu3 = new Button("Help");
                  toolBar.getItems().addAll(menu1, menu2, menu3);