Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8133560

Menu are disapearing when clicked

    XMLWordPrintable

Details

    • Bug
    • Resolution: Cannot Reproduce
    • P4
    • None
    • 8
    • javafx
    • x86_64
    • windows_7

    Description

      Run this sample program : "

      import javafx.application.Application;
      import static javafx.application.Application.launch;
      import javafx.event.Event;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.stage.Stage;

      public class TestMenuBar extends Application {

          public static void main(String[] args) {
              launch(args);
          }

          @Override
          public void start(Stage primaryStage) throws Exception {
              MenuBar menuBar = new MenuBar();

              Menu menu = new Menu("normal");
              Menu subMenu = new Menu("click me I don't disappear");
              menu.getItems().add(subMenu);

              Menu menu2 = new Menu("bug");
              Menu subMenu2 = new Menu("click me I disappear!");
              menu2.setOnShowing(new EventHandler<Event>() {

                  @Override
                  public void handle(Event event) {
                      menu2.getItems().clear();
                      menu2.getItems().add(subMenu2);
                  }
              });

              menu2.getItems().add(subMenu2);

              menuBar.getMenus().addAll(menu, menu2);

              Scene scene = new Scene(menuBar);
              primaryStage.setScene(scene);
              primaryStage.show();

          }
      }
      "

      The subMenu of the first menu is not disappearing when clicked, but the second yes! Why is the normal behavior?

      The reason is that the newly added MenuItem are triggering the menuItemListener in the MenuBarSkin :
      "private ListChangeListener<MenuItem> menuItemListener = (c) -> {
              while (c.next()) {
                  for (MenuItem mi : c.getAddedSubList()) {
                      mi.addEventHandler(ActionEvent.ACTION, menuActionEventHandler);
                  }
                  for (MenuItem mi: c.getRemoved()) {
                      mi.removeEventHandler(ActionEvent.ACTION, menuActionEventHandler);
                  }
              }
          };"

      And this menuActionEventHandler is unselecting menus for an unknown reason :
      "// RT-20411 : reset menu selected/focused state
          private EventHandler<ActionEvent> menuActionEventHandler = t -> {
              if (t.getSource() instanceof CustomMenuItem) {
                  // RT-29614 If CustomMenuItem hideOnClick is false, dont hide
                  CustomMenuItem cmi = (CustomMenuItem)t.getSource();
                  if (!cmi.isHideOnClick()) return;
              }
              unSelectMenus();
          };"

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            shadzic Samir Hadzic
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: