Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定是否在JavaFX中使用了事件_Java_Javafx - Fatal编程技术网

确定是否在JavaFX中使用了事件

确定是否在JavaFX中使用了事件,java,javafx,Java,Javafx,我正在尝试用JavaFX中的事件处理做一些离谱的事情。我需要能够确定在手动触发事件后是否已使用该事件 在下面的示例中,正确接收到合成鼠标事件,但是调用consume()不会更新该事件 我对此进行了调试,发现JavaFX实际上创建了一个新的事件实例,因此原始事件实例保持不变 public class EventManipulation extends Application { public static void main(String[] args) { launch

我正在尝试用JavaFX中的事件处理做一些离谱的事情。我需要能够确定在手动触发事件后是否已使用该事件

在下面的示例中,正确接收到合成鼠标事件,但是调用consume()不会更新该事件

我对此进行了调试,发现JavaFX实际上创建了一个新的事件实例,因此原始事件实例保持不变

public class EventManipulation extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button();
        button.setOnMouseDragged(event -> {
            System.out.println("dragged");
            event.consume();
        });
        primaryStage.setScene(new Scene(new HBox(button), 400, 300));
        primaryStage.show();

        MouseEvent event = new MouseEvent(MouseEvent.MOUSE_DRAGGED, 0, 0, 0, 0, MouseButton.PRIMARY, 1, false, false,
                false, false, false, false, false, false, false, false, null);
        Event.fireEvent(button, event);
        System.out.println(event.isConsumed());  // <== prints false
    }
}

我唯一的解决方案就是实现接口。一个相当小的接口如下所示。不幸的是,javafx使用的内置版本位于一个不可访问的包中-
com.sun.javafx.event.EventDispatchChainImpl

private class SimpleChain implements EventDispatchChain {

    private Deque<EventDispatcher> dispatchers = new LinkedList<>();

    @Override
    public EventDispatchChain append(EventDispatcher eventDispatcher) {
        dispatchers.addLast(eventDispatcher);
        return this;
    }

    @Override
    public EventDispatchChain prepend(EventDispatcher eventDispatcher) {
        dispatchers.addFirst(eventDispatcher);
        return this;
    }

    @Override
    public Event dispatchEvent(Event event) {
        if (dispatchers.peekFirst() != null) {
            Event result = dispatchers.removeFirst().dispatchEvent(event, this);
            if (result != null) {
                return result;
            } else {
                event.consume();
                return event;
            }
        } else {
            return event;
        }
    }
}

您在哪个线程上启动,事件在哪个线程上调度?可能在JavaFX线程上添加Thread.Sleep()?@alex440。。。Application.start()始终在JavaFX线程上运行。@alex440-如果fx内部复制事件,则不会有帮助,或者?我认为函数在事件有机会处理之前终止。
private class SimpleChain implements EventDispatchChain {

    private Deque<EventDispatcher> dispatchers = new LinkedList<>();

    @Override
    public EventDispatchChain append(EventDispatcher eventDispatcher) {
        dispatchers.addLast(eventDispatcher);
        return this;
    }

    @Override
    public EventDispatchChain prepend(EventDispatcher eventDispatcher) {
        dispatchers.addFirst(eventDispatcher);
        return this;
    }

    @Override
    public Event dispatchEvent(Event event) {
        if (dispatchers.peekFirst() != null) {
            Event result = dispatchers.removeFirst().dispatchEvent(event, this);
            if (result != null) {
                return result;
            } else {
                event.consume();
                return event;
            }
        } else {
            return event;
        }
    }
}
Event result = button.buildEventDispatchChain(new SimpleChain()).dispatchEvent(event);
System.out.println(result.isConsumed());