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

Sub Menu Fails to show when ContextMenu is rebuilt by clearing and re-adding MenuItems

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8u20
    • 8u20
    • javafx
    • Java 8u-dev repo tip, Linux

      I believe a recent fix has caused sub menus to fail to show in ContextMenus.

      To reproduce run the test class below:

      1. Right cick on the label and mouse over the sub menu to display it.

      2. Click on the Rebuild button.

      3. Repeat step 1.

      ********************************************* Test Class **********************************************
      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.Label;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.Region;
      import javafx.scene.layout.StackPane;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class ContextMenuTest extends Application {

         @Override public void start(final Stage primaryStage) throws Exception {

            primaryStage.centerOnScreen();
            primaryStage.setHeight(350);
            primaryStage.setWidth(500);
            final Menu submenu = new Menu("Sub Menu");
            submenu.getItems().add(new MenuItem("Test 1"));
              
            Label contextMenuLabel = new Label("Right Click On Me!");
            final ContextMenu contextMenu = new ContextMenu(submenu);
            contextMenuLabel.setContextMenu(contextMenu);
             
            Button button = new Button("Rebuild Menu");
            button.setOnAction(new EventHandler<ActionEvent>() {
               @Override public void handle(ActionEvent event) {
                  contextMenu.getItems().clear();
                  contextMenu.getItems().add(submenu);
               }
            });
            VBox box = new VBox(20, contextMenuLabel, button);
            box.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
             
            primaryStage.setScene( new Scene( new StackPane( box ) ) );

            primaryStage.show();

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

      }

            jgiles Jonathan Giles
            csmithjfx Charles Smith (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: