Henri Augusto
Henri Augusto

Reputation: 339

How to detect mouse drag events on Canvas using JavaFX

This JavaFX app contains a Canvas object (black bkg) on the middle of the window and i'm trying to detect mouse input on that canvas. I've added a simple print to the console action in response to each mouse event just for testing. The below works:

cnv.setOnMouseClicked();

but the following are not working:

cnv.setOnMouseDragOver();
cnv.setOnMouseDragEntered();
cnv.setOnMouseDragReleased();
cnv.setOnMouseDragExited();

Doesn't matter how i click-and-drag the mouse, only mouse clicks are detected ("mouseClicked" appears on the console).

package javafxtest1;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;

public class JavaFXTest1 extends Application {

    @Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();
        Scene scene = new Scene(root, 800, 640);
        Canvas cnv = new Canvas(scene.getWidth()-200,scene.getHeight()-200);
        root.getChildren().add(cnv);

        //Fill the canvas with a black bkg so we can know where it is
        GraphicsContext gc = cnv.getGraphicsContext2D();
        gc.setFill(Color.BLACK);
        gc.fillRect(0, 0, cnv.getWidth(), cnv.getHeight());

        //set response to mouse events
        cnv.setOnMouseClicked((MouseEvent event) -> {
            System.out.println("mouseClicked");
        });
        cnv.setOnMouseDragOver((MouseDragEvent event) -> {
            System.out.println("mouseDragOver");
        });
        cnv.setOnMouseDragEntered((MouseDragEvent event) -> {
            System.out.println("mouseDragEntered");
        });
        cnv.setOnMouseDragReleased((MouseDragEvent event) -> {
            System.out.println("mouseDragReleased");
        });
        cnv.setOnMouseDragExited((MouseDragEvent event) -> {
            System.out.println("mouseDragExited");
        });


        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Where is my mistake?

(I'm on Win 7 64 with NetBeans 8.2)

Upvotes: 3

Views: 10442

Answers (1)

James_D
James_D

Reputation: 209235

Those event handlers are for platform supported drag and drop handling, which is initiated if you call startDragAndDrop() in a drag detected event. (They are also only fired under certain circumstances, such as when the dragboard is not empty.)

You should just use setOnMouseDragged() for regular mouse event handling.

See the "Dragging Gestures" section of the MouseEvent documentation for details of the different mouse drag modes and the events they support.

Upvotes: 4

Related Questions