JavaFX 2 event dispatching to underlying nodes

雨燕双飞 提交于 2019-11-29 14:20:13

By default events will just propagate up the heirarchy and terminate at the root. There are a few approaches you could take to solve your problem.

  1. Create your own event instance. Add an event handler to both regions that triggers your shared event instance. Add any event handling code you want to be common across regions to the shared instance. This is the approach I would take from the description you've given.
  2. Catch all events at the root and, instead of just letting them die, create a global event register that everyone can register for.
  3. Create an event handler at the first region and catches events and redispatches them at the second region (using buildEventDispatchChain.dispatchEvent). Then do the same on the other side.

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.

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;
}

Just catch the event in an event handler and fire it on the other components:

top.addEventHandler(EventType.ROOT, event -> bottom.fireEvent(event));

You can still add mouse listeners on the top components and it works fine. If the bottom component does more fancy stuff with the event, you might need to clone and adjust it. This also works with more than two children.

krivanker

Trying this might also work,

p1.setEventDispatcher(p2.eventDispatcherProperty().get()); 

EventDispatcher Interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!