Mouseevent – get mouse from a node_ After the pressed event, the mouse event cannot be obtained from any other JavaFX 8 node

I'm creating a rich text component that contains the selection function of JavaFX projects and face some difficulties

The following is a similar example of a label:

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.V@R_496_2419@;
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));

        V@R_496_2419@ mainVB = new V@R_496_2419@();
        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 the mouse over a different label and viewing the message on the command line Then press and hold the main mouse button on any label and move it again Before you release the button, you will see that no other tag will trigger any event

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

I also try to manually trigger the mouse of the corresponding tag_ Released, but it doesn't help

Thank you for your support

Solution

Document for mouseevent details three different modes for handling mouse drag In the default mode ("simple press drag release gesture"), as you can see, mouse events are only passed to the node from which the gesture originated

In "fully Press - drag release gesture" mode, mousedragevents are passed to other nodes during drag This is the mode you need. You can activate it by calling startfulldrag on the original node

(the third mode is the "drag and drop" gesture, which is used to transfer data between nodes. It is usually supported by the underlying platform, so you can drag and drop between JavaFX applications and other applications and within applications.)

Try the following code for your event handler:

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()));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>