简体   繁体   English

倾听听众

[英]Listening for a listener

Class foo
{
    public void functionA()
    // do stuff
}

class bar
{
    private foo fooClass = new foo();

    private listener on foo.functionA() call
    {
         // do stuff
    }
}

What I want to know is if this is even possible. 我想知道的是,是否有可能。

I'm currently using javafx, I have a group which contains a bunch of cylinders and when a cylinder is dragged I would like to call a function from a node above within the scene graph. 我目前正在使用javafx,我有一个包含一堆圆柱体的组,当拖动圆柱体时,我想从场景图中上方的节点调用函数。 The issue is I am not sure how far into the scene graph the cylinders are going to be, so calling .getParent() may not be the best course of action. 问题是我不确定圆柱体将要进入场景图形的距离,因此调用.getParent()可能不是最佳方法。

The main thing I am considering is the possibility of a listener listening for the event within the cylinder. 我要考虑的主要问题是听众有可能在圆柱体内监听事件。

I'm not sure if this is the best explanation so sorry if it is confusing. 我不确定这是否是最好的解释,如果造成混淆,请您谅解。

Essentially I am trying to edit a class when a listener is called. 本质上,我在尝试调用侦听器时尝试编辑一个类。 The class I am trying to edit has access to the listener class but not vice versa. 我尝试编辑的类可以访问侦听器类,反之则不能。

I fixed the issue by sending the eventHandler to the lower object listener. 我通过将eventHandler发送到下层对象侦听器来解决此问题。

Example: 例:

class foo
{
    public void setListeners(EventHandler handler)
    {
        this.setListener(handler);
    }
}

class bar
{
    foo fooObject = new foo();

    EventHandler eventH = new eventhandler()
    { 
        @override
        public void handle(event args)
        {
           functionToDo();
        }
    };

    private void functionToDo(){ do stuff }

    // call in some method
    {
        fooObject.setListeners(eventH);
    }
}

Now when the listener in foo is called (this can be done in many ways, but for my project it is a drag action), functionToDo will be called within bar. 现在,当调用foo中的侦听器时(可以通过多种方式完成,但是对我的项目来说这是一个拖动动作),functionToDo将在bar中调用。

I'm sure there is a better fix but this worked for me. 我确定有更好的解决方案,但这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM