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

[TextField] opening the default context menu by mouse allows multiple context menus to be open

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4
    • tbd
    • 8u5, 8u20
    • javafx
    • None

    Description

      opening the default context menu by mouse allows multiple context menus to be open (related issue, probably with the same underlying reason: https://javafx-jira.kenai.com/browse/RT-26028

      To reproduce, run the example and
      1. right-click on the HBox to get the "some parent action" context menu.
      2. right-click on the TextFiled to get the TextField's context menu

      If done the other way around (TextField's context menu first, then HBox's) the TextField context menu closes as expected.

      This issue was one of three reported in RT-37519

      /*
       * Created on 12.06.2014
       *
       */
      package chapter4layoutandcontrols;


      import java.util.logging.Logger;

      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.geometry.Insets;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.Label;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.TextField;
      import javafx.scene.input.ContextMenuEvent;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.Pane;
      import javafx.stage.Stage;


      /**
       * Issues with default context menu:
      * - does not open on keystroke (shift-f10 on windows)
      * - doesn't consume contextMenuEvents, leading to multiple open contextMenus
      *
       * @author Jeanette Winzenburg, Berlin
       */
      public class ContextMenuExample extends Application {

          private Parent getContent() {
              Label label = new Label("me and ancient queens");
              // BUG: TextField doesn't open default contextMenu on shift-f10
              // implemented in TextFieldBehaviour: uses MouseEvent
              TextField textField = new TextField("some text");
              // a menu installed by client code is handled correctly
              //ContextMenu customMenu = new ContextMenu(new MenuItem("some local action"));
              //textField.setContextMenu(customMenu);
              Pane parent = new HBox(100);
              ContextMenu parentMenu = new ContextMenu(new MenuItem("some parent action"));
              // a handler on the parent that's invoked if none of the children
              // has handled the event
              // reacts to shift-f10, opens on right pressed as expected
              // REMINDER to self: need a handler on the parent because setContextMenu is defined only for
              // Controls
              EventHandler<ContextMenuEvent> paneHandler = e -> {
                  parentMenu.show(parent, e.getScreenX(), e.getScreenY()-100);
                  LOG.info("parentHandler " + e);
              };
              // BUT: if right-pressed in textField with default context, we have two context menus
              parent.addEventHandler(ContextMenuEvent.ANY, paneHandler);
              parent.getChildren().addAll(label, textField);
              parent.setPadding(new Insets(20));
              return parent;
          }

          @Override
          public void start(Stage primaryStage) throws Exception {
              Scene scene = new Scene(getContent());
              primaryStage.setScene(scene);
              primaryStage.show();
          }

          public static void main(String[] args) {
              launch();
          }
          
          @SuppressWarnings("unused")
          private static final Logger LOG = Logger.getLogger(ContextMenuExample.class
                  .getName());
      }

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              dgrieve David Grieve
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Imported: