从一个节点获取MOUSE_PRESSED事件后,无法从任何其他javafx 8节点获取鼠标事件 [英] Can't get mouse event from any other javafx 8 node after getting MOUSE_PRESSED event from one node

查看:145
本文介绍了从一个节点获取MOUSE_PRESSED事件后,无法从任何其他javafx 8节点获取鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为JavaFX项目创建具有选择功能的RTF组件,但遇到了一些困难. 我试图抓住哪个TextFlow对象用户按下鼠标按钮,并在另一个TextFlow对象上释放它.但是在发生MOUSE_PRESSED事件后,我只能与触发它的TextFlow交互,直到释放鼠标为止.

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.

我还尝试为相应的标签手动触发MOUSE_RELEASED,但这也无济于事.

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

感谢您的支持.

推荐答案

文档详细介绍了三种处理鼠标拖动的方式.如您所见,在默认模式(简单的按下并释放手势")中,鼠标事件仅传递到该手势所源自的节点.

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.

在完全按下-释放-释放手势"模式下,MouseDragEvent s在拖动过程中会传递到其他节点.这是您需要的模式,可以通过在原始节点上调用startFullDrag来激活它.

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.

(第三个模式是拖放"手势,该手势用于在节点之间传输数据,通常受基础平台支持,因此您可以在JavaFX应用程序与其他应用程序以及其他应用程序之间进行拖放在应用程序中.)

(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()));

这篇关于从一个节点获取MOUSE_PRESSED事件后,无法从任何其他javafx 8节点获取鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆