2
votes

I tried almost everything, but the mouse drag events are not firing, like explained here:

https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/input/MouseDragEvent.html

Here is a minimal example, so you can try it out (I am using Java 11 with JavaFX 11.0.2):

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.stage.Stage;


public class Main extends Application {

    private double mouseClickPositionX, mouseClickPositionY, currentRelativePositionX, currentRelativePositionY;

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("Hello World");

        BorderPane mainBorderPane = new BorderPane();
        BorderPane centerBorderPane = new BorderPane();
        FlowPane flowPane = new FlowPane();
        GridPane gridPane = new GridPane();
        Button button1 = new Button("button1");
        gridPane.add(button1, 0, 0);
        flowPane.getChildren().add(gridPane);
        centerBorderPane.setCenter(flowPane);
        HBox hbox = new HBox();
        TilePane tilePane = new TilePane();
        Button button2 = new Button("button2");
        tilePane.getChildren().add(button2);
        hbox.getChildren().add(tilePane);
        mainBorderPane.setCenter(centerBorderPane);
        centerBorderPane.setBottom(hbox);

        // button2 event handlers

        button2.setOnMousePressed(event -> {
            mouseClickPositionX = event.getSceneX();
            mouseClickPositionY = event.getSceneY();
            currentRelativePositionX = button2.getTranslateX();
            currentRelativePositionY = button2.getTranslateY();
            button2.setMouseTransparent(true);
        });

        button2.setOnMouseDragged(event -> {
            button2.setTranslateX(currentRelativePositionX + (event.getSceneX() - mouseClickPositionX));
            button2.setTranslateY(currentRelativePositionY + (event.getSceneY() - mouseClickPositionY));
        });

        button2.setOnDragDetected(event -> {
            button2.startFullDrag();
        });

        button2.setOnMouseReleased((event) -> {
            button2.setMouseTransparent(false);
        });

        // button1 event handlers

        button1.setOnMouseDragReleased((event) -> {
            System.out.println("it works in button1");
        });

        // gridPane event handlers

        gridPane.setOnMouseDragReleased((event) -> {
            System.out.println("it works in gridPane");
        });

        primaryStage.setScene(new Scene(mainBorderPane, 300, 275));
        primaryStage.show();
    }


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

I want to get the reference of button2 either in button1 or in gridPane via setOnMouseDragReleased. There are many nested panes etc. because I wanted to maintain the original project layout structure. I did this because I am not sure if this also can be a reason for the non functioning.

Thanks in advance.

2
Not 100% sure, but it could be due to the buttons have a GridPane parent. Try adding them a regular Pane.Sedrick
@Sedrick tried changing the GridPane to a regular Pane but still not workingJamal B.

2 Answers

1
votes

I've ended up manually triggering the events from centerBorderPane to gridPane, using node.fireEvent(event). Also implemented a helper function, which returns the right child node:

  private Optional<Node> findNode(Pane pane, double x, double y) {
    return pane.getChildren().stream().filter(n -> {
      Point2D point = n.sceneToLocal(x, y);
      return n.contains(point.getX(), point.getY());
    }).findAny();
  }

Don't forget to consume the events, so you won't get into an infinite loop.

0
votes

MOUSE_DRAG_RELEASED fires when a drag ends on this node. For example

 centerBorderPane.setOnMouseDragReleased((event) -> {
        System.out.println("centerBorderPane drag released");
 });

should fire when you drag button2 and the drag ends on centerBorderPane.

To fire an event when the mouse is dragged over button1 use button1.setOnMouseDragged
If you want to propagate a mouse event from parent to its children see this