@FXML是否有初始化的副本? [英] Is there a counterpart to @FXML initialize?

查看:62
本文介绍了@FXML是否有初始化的副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个视图被销毁(阶段关闭,另一个视图取代了它等等)时,控制器上是否有任何方法会被调用?也就是说,是否有与之对应的内容:

When a view is destroyed (stage closed, another view taken its place, etc), are there any methods that will get called on the controller? That is, is there a counterpart to:

@FXML
private void initialize()

更新:执行此操作的原因是我的应用程序正在从PubNub接收消息,然后需要将这些消息中继到这些控制器,以便他们可以选择是否对它们执行操作或不(它们是否确实取决于内部控制器信息,例如视图的大小).

Update: The reason why I'm doing this is that my application is receiving messages from PubNub and then those messages need to be relayed to these controllers so they can chose whether to act on them or not (whether they do depends on internal controller information, such as the size of the view).

我的计划是让这些控制器在全局列表中注册自己,并在销毁时注销自己.

My plan was to have these controllers register themselves in a global list and when destroyed, unregister themselves.

推荐答案

大多数注释告诉您JavaFX中没有此类等效功能. James_D 也提到了使用WeakReference的方法,我相信这是解决您问题的好方法.

Most of the comments have told you there is no such equivalent feature in JavaFX. James_D has also mentioned using WeakReference, which I believe is a good solution for your problem.

public final class PubNubManager {
    private final List<WeakReference<PubNubListener>> listeners = new ArrayList<>();

    // Other standard singleton stuff

    public void informListeners(PubNubData data) {
        listeners.stream().forEach(listenerRef -> {
            if (listenerRef.get() != null)
                listenerRef.get().receiveData(data);
        });

        // You can remove weakreference that are no longer valid, or stop getting for data when there are no more listener.
    }

    public void registerListener(PubNubListener listener) {
        if (listener != null)
            listeners.add(new WeakReference<>(listener));
    }

    // You can make one to unregister too if you want.
}

public interface PubNubListener {
    void receiveData(PubNubData data);
}

public class ControllerA implements PubNubListener {
    @Override
    public void receiveData(PubNubData data) {
        // What you need to do
    }
}

这篇关于@FXML是否有初始化的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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