Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

mouseevent - Can't get mouse event from any other javafx 8 node after getting MOUSE_PRESSED event from one node

I'm creating rich text component with selection capabilities for JavaFX project and facing some difficulties. I'm trying to catch on which TextFlow object user presses mouse button and on which another TextFlow he releases it. But after MOUSE_PRESSED event i can interact only with that TextFlow, who fired it, until i release the mouse.

Here is similar 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 to move mouse over different Labels and watch messages in command line. After that press and hold mouse primary button on any Label and move it again. You'll see that no other Labels will fire any event until you releases the button.

I spend some time searching for the solution but got nothing.

I also tried to manually fire MOUSE_RELEASED for corresponding Label but it didn't help also.

Appreciate your support.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The documentation for MouseEvent details three different modes for handling mouse drag. In the default mode ("simple press-drag-release gesture"), as you've observed, mouse events are delivered only to the node on which the gesture originated.

In "full press-drag-release gesture" mode, MouseDragEvents are delivered to other nodes during the drag. This is the mode you need, and you activate it by calling startFullDrag on the originating node.

(The third mode is "drag-and-drop" gesture, which is for transferring data between nodes and is typically 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 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()));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...