javafx有一个eventfilter自身删除 [英] javafx have an eventfilter remove itself

查看:627
本文介绍了javafx有一个eventfilter自身删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码片段如下:

//button clicked method
@FXML
public void newHeatExchanger2ButtonClicked(ActionEvent event) throws Exception {

    //new pane created
    Pane pane = new Pane();

    //method call everytime the button is clicked
    create2DExchanger(pane);

   }

//method declaration
private void create2DExchanger(Pane pane) {

    EventHandler<MouseEvent> panePressed = (e -> {
        if (e.getButton() == MouseButton.SECONDARY){

            do stuff
        }

        if (e.getButton() == MouseButton.PRIMARY){
            do stuff
        }
    });

    EventHandler<MouseEvent> paneDragged = (e -> {
        if (e.getButton() == MouseButton.PRIMARY){
            do stuff
        }
    });
    EventHandler<MouseEvent> paneReleased = (e -> {
        if (e.getButton() == MouseButton.PRIMARY){
            do stuff;
        }

    });
    EventHandler<MouseEvent> paneMoved = (t -> {
        do stuff;
    });
    EventHandler<MouseEvent> paneClicked = (t -> {
        //I need this filter to remove itself right here
        t.consume();
        pane.removeEventFilter(MouseEvent.MOUSE_MOVED, paneMoved);
        pane.addEventHandler(MouseEvent.MOUSE_PRESSED, panePressed);
        pane.addEventHandler(MouseEvent.MOUSE_DRAGGED, paneDragged);
        pane.addEventHandler(MouseEvent.MOUSE_RELEASED, paneReleased);
    });
    pane.removeEventHandler(MouseEvent.MOUSE_PRESSED, panePressed);
    pane.removeEventHandler(MouseEvent.MOUSE_DRAGGED, paneDragged);
    pane.removeEventHandler(MouseEvent.MOUSE_RELEASED, paneReleased);
    pane.addEventFilter(MouseEvent.MOUSE_MOVED, paneMoved);
    pane.addEventFilter(MouseEvent.MOUSE_PRESSED, paneClicked);


}

最初我将窗格设置为仅mouse_moved和mouse_pressed的事件过滤器。一旦鼠标点击,我需要鼠标过滤器mouse_pressed和mouse_moved来消除,并添加eventHandlers像我在窗格单击过滤器llamda。我需要第一组事件作为过滤器,因为有孩子节点我不想接收事件(即在窗格的孩子的弧上的事件过滤器)。第二组需要处理程序,因为arc事件过滤器需要在事件窗格接收到事件之前消耗事件。

Initially I set the pane to have only the event filters of mouse_moved, and mouse_pressed. As soon as the mouse is clicked I need the mouse filter for mouse_pressed and mouse_moved to go away and add the eventHandlers as I do in the paneClicked filter llamda. I need the first set of events to be filters because there are children nodes I do not want to receive the event (i.e. an event filter on an arc that is a child of the pane). The second set need to be handlers because the arc event filter needs to consume the event before the pane eventHandlers receive it.

便利事件如:

    pane.setOnMousePressed() 

可以通过调用

    pane.setOnMousePressed(null); 

但是我需要这个初始事件过滤器来删除自己。我将需要在代码中删除自己的事件的功能,但是如果我尝试添加

but I need this initial event filter to remove itself. I will need the functionality of an event removing itself later as well in the code, but if I try to add

    pane.removeEventFilter(MouseEvent.MOUSE_PRESSED, paneClicked);

    EventHandler<MouseEvent> paneClicked = (t -> {
        //I need this filter to remove itself right here
        t.consume();
        pane.removeEventFilter(MouseEvent.MOUSE_MOVED, paneMoved);
        pane.addEventHandler(MouseEvent.MOUSE_PRESSED, panePressed);
        pane.addEventHandler(MouseEvent.MOUSE_DRAGGED, paneDragged);
        pane.addEventHandler(MouseEvent.MOUSE_RELEASED, paneReleased);
    });

它不会编译。我一直在研究几天,了解如何获取eventFilter或eventHandler的功能,以消除自身,但我很快就会发现。我还没有在我的Google搜索中找到任何网络或stackexchange。我真的很感激能够把这件事看出来。谢谢。

It will not compile. I have been researching for a couple of days on how to get the functionality of an eventFilter or eventHandler to remove itself but I am coming up short. I have not found anything online or on stackexchange in my Google searches either. I would really appreciate being able to figure this thing out. Thanks.

推荐答案

我相信这个问题起因于你试图访问 paneClicked 在完全声明之前。

I believe the problem stems from the fact that you try to access paneClicked before it was fully declared.

您可以使用关键字的匿名类来克服此问题:

You can overcome this using an anonymous class with the this keyword:

EventHandler<MouseEvent> paneClicked =  new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        event.consume();
        pane.removeEventFilter(MouseEvent.MOUSE_PRESSED, this);
        pane.removeEventFilter(MouseEvent.MOUSE_MOVED, paneMoved);
        pane.addEventHandler(MouseEvent.MOUSE_PRESSED, panePressed);
        pane.addEventHandler(MouseEvent.MOUSE_DRAGGED, paneDragged);
        pane.addEventHandler(MouseEvent.MOUSE_RELEASED, paneReleased);
    }
};

或通过引用完全限定的静态功能:

Or by referencing a fully qualified static function:

public class ContainingClass {
    ...
    private static EventHandler<MouseEvent> paneClicked = (t -> {
        t.consume();
        pane.removeEventFilter(
            MouseEvent.MOUSE_PRESSED, ContainingClass.paneClicked
        );
    });
}

另见:

  • Why do lambdas in Java 8 disallow forward reference to member variables where anonymous classes don't?
  • Java8 Lambdas vs Anonymous classes

这篇关于javafx有一个eventfilter自身删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆