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

[Events] Simple mouse drag from one node to sibling generates unexpected click event in shared parent

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8u20, 8u40
    • Fix Version/s: 9
    • Component/s: javafx
    • Labels:
    • Environment:

      OSX 10.9.5 and Ubuntu 14.04

      Description

      The documentation for mouse dragging says that "The simple press-drag-release gesture is default... When mouse button is pressed, the top-most node is picked and all subsequent mouse events are delivered to the same node until the button is released. If a mouse clicked event is generated from these events, it is still delivered to the same node."

      However, I see different behaviour. I create an HBox that holds two Labels. I start dragging from the left Label and end in the right Label. The left Label gets the drag-detected and mouse-dragged and mouse-released events. BUT: at the end, the parent HBox also receives a mouse-click event. This is surprising for three reasons. Firstly, I've dragged the mouse about 400 pixels -- that shouldn't count as a click. Secondly, the HBox isn't at all visible beneath the Labels in this example, so it seems wrong that it can receive any mouse events (if anything, I would expect the right Label to get the click). Thirdly, the docs say that any mouse click will be "delivered to the same node", which I take to be the left Label, not the HBox.

      Behaviour confirmed on OS X 10.9.5 with 8u20, and on Ubuntu 14.04 with 8u20 and 8u40-b11. It doesn't seem to make any difference whether or not I consume the earlier mouse events. It's not to do with Label and HBox per se, I've also seen this behaviour in my real application with TextField and VBox. Code to reproduce (drag from left-hand blue label to right-hand pink label):

      package test;

      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.layout.Background;
      import javafx.scene.layout.BackgroundFill;
      import javafx.scene.layout.HBox;
      import javafx.scene.paint.Color;
      import javafx.stage.Stage;

      public class TestDrag extends Application
      {

          @Override
          public void start(Stage primaryStage) throws Exception
          {
              String leftHead = "Start dragging from me\n";
              String dragStarted = "Drag begun; staying simple\n";
              Label left = new Label(leftHead);

              // Makes no difference if I consume pressed event:
              // left.setOnMousePressed(e -> e.consume());
              // Nor if I consume a clicked event:
              // left.setOnMouseClicked(e -> e.consume());
              
              left.setOnDragDetected(e -> {
                  left.setText(leftHead + dragStarted);
                  e.consume();
              });
              
              left.setOnMouseDragged(e -> {
                  left.setText(leftHead + dragStarted + "Mouse dragged to: " + e.getSceneX() + ", " + e.getSceneY());
                  e.consume();
              });
              
              left.setOnMouseReleased(e -> {
                  left.setText(leftHead + "Mouse released\n");
                  e.consume();
              });
              
              String rightHead = "Drag on to me\n";
              Label right = new Label(rightHead);
              right.setOnMouseClicked(e -> {
                  right.setText(rightHead + "Clicked me!\n");
              });
              
              left.setPrefSize(400, 300);
              left.setBackground(new Background(new BackgroundFill(Color.LIGHTBLUE, null, null)));
              right.setPrefSize(400, 300);
              right.setBackground(new Background(new BackgroundFill(Color.LIGHTPINK, null, null)));
              
              HBox hbox = new HBox(left, right);
              hbox.setOnMouseClicked(e -> {
                  right.setText(rightHead + "Clicked the underlying HBox at " + System.currentTimeMillis() + "\n");
              });
              
              primaryStage.setScene(new Scene(hbox));
              primaryStage.show();
          }

      }

        Attachments

          Activity

            People

            • Assignee:
              ckyang Chien Yang (Inactive)
              Reporter:
              nbrownjfx Neil Brown (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported: