确定从节点内部关闭JavaFX Stage的时间

问题描述 投票:0回答:1

我有一个SplitPane,其中我有显示动态数据的选项卡,所以每当我关闭一个选项卡时,我想确保关闭所有数据连接。这是通过在选项卡中侦听节点的sceneProperty来完成的,当SceneProperty更改为null时,连接将关闭,因为选项卡已关闭。

将标签从SplitPane移除到自己的窗口的可能性改变了一切。现在,当SceneProperty更改时,任务计划等待一秒,然后检查节点上的SceneProperty是否仍然为空,然后关闭连接。所有这些都是为了避免在选项卡转移到自己的窗口时关闭连接,因为在此期间,SceneProperty非常简短地变为null。这仍然适用于关闭选项卡,但奇怪的是,当节点有自己的窗口时它不起作用,显然这是因为即使窗口关闭后窗口的rootNode上的SceneProperty也不是null。

而现在我想知道它会怎样......?

这基本上都是所有代码

// somewhere in the constructor
rootNode.sceneProperty().addListener((observable, oldValue, newValue) -> subscriptionChange(newValue));

private void subscriptionChange(Scene newValue) {
    if (newValue == null) {
        scheduleUnsubscribeTask();
    }
}

private void scheduleUnsubscribeTask() {
    UnsubscribeTask unsubscribeTask = new UnsubscribeTask(this::handleUnsubscription);
ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(1);
scheduledPool.schedule(unsubscribeTask, TTL_MS, TimeUnit.MILLISECONDS);
}

private void handleUnsubscription() {
    if (paneMain.getScene() == null) {
       closeConnection();
   }
}
javafx-8
1个回答
0
投票

而现在我想知道它会怎样......?

关闭Window对相关的Scene没有任何作用。 scene属性也独立于被添加到窗口的场景;它只是表明,如果节点所在的节点结构的根是场景的根。

如果要检查,如果节点是显示场景的一部分,我建议使用Bindings.selectBoolean。否则你需要(联合国)注册/移动3个听众(Node.sceneScene.windowWindow.showing属性)。

private BooleanBinding showing;
showing = Bindings.selectBoolean(button.sceneProperty(), "window", "showing");
showing.addListener((o, oldValue, newValue) -> {
    if (!newValue) {
        scheduleUnsubscribeTask();
    }
});

...

private void handleUnsubscription() {
    if (!showing.get()) {
       closeConnection();
   }
}

但是,我建议为您的场景内容引入一些生命周期。这样您就可以完全控制处理场景所需的资源。

© www.soinside.com 2019 - 2024. All rights reserved.