Can't get mouse event from any other javafx 8 node after receiving MOUSE_PRESSED event from one node

I am creating a rich text component with highlighting capabilities for a JavaFX project and am facing some difficulties. I am trying to catch on which TextFlow the user is clicking on and on which other TextFlow they are releasing it. But after the MOUSE_PRESSED event, I can only interact with the TextFlow that fired it until I release the mouse.

Here's an example with labels:

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        AnchorPane root = new AnchorPane();
        primaryStage.setTitle("Events Problem Example");
        primaryStage.setScene(new Scene(root, 800, 600));

        VBox mainVB = new VBox();
        root.getChildren().add(mainVB);

        //########## Code is here:
        for (int i = 0; i < 5; i++) {
            final Label label = new Label("labelβ„–"+i);
            mainVB.getChildren().addAll(label);

            label.setOnMouseEntered(mouseEvent -> System.out.println("entering " + label.getText()));
            label.setOnMousePressed(mouseEvent -> System.out.println("press mouse button on " + label.getText()));
            label.setOnMouseReleased(mouseEvent -> System.out.println("release mouse button on " + label.getText()));
        }
        //########################

        primaryStage.show();
    }


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

      

Try moving your mouse to different shortcuts and see the messages on the command line. After that, press and hold the primary mouse button on any shortcut and move it again. You will see that no other shortcuts will fire any event until you release the button.

I spent some time looking for a solution but got nothing.

I also tried to manually trigger MOUSE_RELEASED on the corresponding label, but that didn't help either.

Rate your support.

+3


source to share


1 answer


The documentation forMouseEvent

contains three different modes for handling drag and drop. In the default mode ("simple click-to-drag" gesture), as you can see, mouse events are sent only to the node on which the gesture was created.

In full click-and-drop mode, they are MouseDragEvent

delivered to other nodes while dragging. This is the mode you need, and you activate it by calling startFullDrag

the source node.

(The third mode is the drag-and-drop gesture, which is for transferring data between nodes and is usually supported by the underlying platform, so you can drag and drop between your JavaFX application and other applications, as well as within the application.)



Try using the following code for your event handlers:

        label.setOnDragDetected(mouseEvent -> label.startFullDrag());
        label.setOnMouseDragEntered(mouseEvent -> System.out.println("entering " + label.getText()));
        label.setOnMouseDragReleased(mouseEvent -> System.out.println("release mouse button on " + label.getText()));

      

+7


source







All Articles