【问题标题】:Make mouse events detectable on whole node stack在整个节点堆栈上检测到鼠标事件
【发布时间】:2017-11-07 14:53:11
【问题描述】:

我有问题,我有BorderPane,在哪个中心有TabPane 和Tabs,还有其他各种Nodes(例如另一个BorderPane)。

当我尝试例如使用时:

parentBorderPane.setOnMouseDragged(e -> System.out.println("DRAGGING"));
parentBorderPane.setOnMouseMoved(e -> System.out.println("MOVING"));

...事件永远不会发生(我可以在当前打开的 Tab's 内容上捕捉到它们,但这不是我想要的)。

我需要知道此类事件何时在两个地方发生(第一个事件通过Tab 的内容,然后通过包含TabPane,然后通过主要父级BorderPane)。所以我不能使用(对任何孩子):

this.setMouseTransparent(true);

如何解决?

编辑

让我补充一点,主要问题在于TabPane - 它不会将事件传递到堆栈中的下一个节点。

编辑 2

public class T extends Application
{
    public static void main(String[] args) throws Exception
    {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception
    {
        stage.setTitle("Test");
        Scene scene = new Scene(this.working()); // change to this.problem();
        stage.setScene(scene);
        stage.show();
    }

    private BorderPane working()
    {
        BorderPane main = new BorderPane();
        main.setPrefSize(800, 600);
        main.setOnMouseMoved(e -> System.out.println("MOVING ON MAIN"));

        BorderPane sub = new BorderPane();
        sub.setOnMouseMoved(e -> System.out.println("MOVING ON SUB"));
        main.setCenter(sub);

        return main;
    }

    private BorderPane problem()
    {
        BorderPane main = new BorderPane();
        main.setPrefSize(800, 600);
        main.setOnMouseMoved(e -> System.out.println("MOVING ON MAIN"));

        BorderPane sub = new BorderPane();
        sub.setOnMouseMoved(e -> System.out.println("MOVING ON SUB"));
        main.setCenter(sub);

        TabPane tabs = new TabPane();
        tabs.setOnMouseMoved(e -> System.out.println("MOVING ON TABS"));
        sub.setCenter(tabs);

        return main;
    }
}

working() - 正常的Panes 相互堆叠将正常工作(将事件从堆栈的顶部传递到底部)。

problem() - 因为TabPane 是 Control 的子类(我目前正在研究),所以它会停止鼠标事件并且不会让父母看到它们。

【问题讨论】:

  • 向我们展示一个显示问题的最小工作程序将帮助我们更快地识别它。
  • @JKostikiadis 添加了示例代码。

标签: java javafx


【解决方案1】:

我找到了解决方案 - 我可以使用 event filters 并在之前捕获事件,而不是尝试接收 Control(TabPane 的父类)消耗它的事件 durnig 冒泡阶段(子->父级)它被消耗掉了。

【讨论】:

猜你喜欢
  • 2014-03-19
  • 2023-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
相关资源
最近更新 更多