JavaFx, event interception/consumption

2019-02-24 11:16发布

I've got this partial scenegraph tree :

CustomPane (with onMouseClicked Handler)
 → ChildNode (with onMousePressed Handler)

When I catch the MousePressed event in the ChildNode, I can consume it, so that the parent doesn't receive a MousePressed event. But I would like to consume the associated MouseClicked event. So that pressing the mouse on the Child doesn't fire a MouseClicked event on the Parent.

1条回答
手持菜刀,她持情操
2楼-- · 2019-02-24 11:40
  1. You can add specific ChildNode#onMouse... handlers which will consume all events.

  2. or provide your own EventDispatcher:

    child.setEventDispatcher(new EventDispatcher() {
    
        @Override
        public Event dispatchEvent(Event event, EventDispatchChain tail) {
            boolean valid = myValidationLogicForEvents(event);
            return valid ? tail.dispatchEvent(event) : null;
        }
    });
    
查看更多
登录 后发表回答