JavaFX 2 事件分派到底层节点

JavaFX 2 event dispatching to underlying nodes(JavaFX 2 事件分派到底层节点)

本文介绍了JavaFX 2 事件分派到底层节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有正确的方法来解决两个同级窗格之间的事件传播问题?

Is there a correct way to solve the problem with event propagation between two sibling panes?

例如,我们有内部有 2 个窗格的 StackPane.

For example we have StackPane with 2 panes inside.

StackPane p = new StackPane();
Region p1 = new Region();
Region p2 = new Region();
p.getChildren().addAll(p1, p2);

此示例中的 p2 捕获鼠标事件,即使未消耗事件,p1 也无法对其做出反应.

p2 in this example capture mouse events and p1 can't react on it even if event is not consumed.

如果事件不被 p2 消费,是否有正确的方法将事件传播到 p1?

Is there a correct way to propagate event to p1 if it not consumed by p2?

setMouseTransparent 不能解决我的问题,因为我需要两个子元素都对鼠标做出反应.

setMouseTransparent not solve my problem because I need that both children elements react on mouse.

感谢您的建议.

推荐答案

我的问题部分解决了.也许我不太正确地提出问题.我编写了图形编辑器之类的应用程序,并在带有指南、网格、选择工具等的 stackpane 上有工具层窗格,并且需要该层的子层可以处理鼠标,并且窗格本身对于鼠标事件是透明的.

My problem was partially solved. Maybe I not quite correctly formulate question. I write app like graphic-editor and have tools-layer panes on stackpane with guides, grid, selection-tools etc. and need that children of this layers can handle mouse and panes itself will be transparent for mouse events.

问题已通过覆盖 pickNode 解决,而不是在公共 API 中,但它可以工作.也许可以帮助某人.

Problem was solved by override pickNode, not in public API, but it work. Maybe help somebody.

protected Node impl_pickNodeLocal(double localX, double localY) {
    if (containsBounds(localX, localY)) {
        ObservableList<Node> children = getChildren();
        for (int i = children.size()-1; i >= 0; i--) {
            Node picked = children.get(i).impl_pickNode(localX, localY);
            if (picked != null) return picked;
        }
        // hack to make pane itself transparent for mouse
        // if (contains(localX, localY)) return this;
    }
    return null;
}

这篇关于JavaFX 2 事件分派到底层节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:JavaFX 2 事件分派到底层节点

基础教程推荐