简体   繁体   English

JavaFX:删除BorderPane中心中加载的场景

[英]JavaFX: Remove the scene loaded in Center of BorderPane

I have a JavaFX2.2 application which has a BorderPane on the main screen. 我有一个JavaFX2.2应用程序,它在主屏幕上有一个BorderPane。 On the top Pane I have two buttons 'Button A' and 'Button B' to dynamically load scenes 'Scene A' and 'Scene B' in the center of the BorderPane respectively. 在顶部窗格中,我有两个按钮“按钮A”和“按钮B”,分别在BorderPane的中心动态加载场景“场景A”和“场景B”。

'Scene A' has two buttons. '场景A'有两个按钮。 One of them defined as 'Default Button' and other as 'Cancel Button' in the FXML file. 其中一个在FXML文件中定义为“默认按钮”,另一个定义为“取消按钮”。

'Scene B' has a TextField and a TableView. 'Scene B'有一个TextField和一个TableView。

Following is the code snippet from the main screen to switch between the scenes. 以下是主屏幕中用于在场景之间切换的代码片段。

@FXML
private void handlebtnAAction(ActionEvent event) {
    loadCentreScene("fxml/FXSceneA.fxml");       
}

@FXML
private void handlebtnBAction(ActionEvent event) {
    LoadCentreScene("fxml/FXSceneB.fxml");
}


private void loadCentreScene(String fxmlPath){
    try {
        FXMLLoader loader = new FXMLLoader(Admin.class.getResource(fxmlPath));
        AnchorPane page = (AnchorPane) loader.load();
        Plugin fxController = loader.getController();
        fxController.setMainController(this);
        Node node = getRootLayout().getCenter();
        node = null;            
        getRootLayout().setCenter(page);            
    } catch (IOException ex) {
        Dialogs.showErrorDialog(primaryStage, ex.getMessage(), "Loading Error");
    }
}

Now when I switch from 'Scene A' to 'Scene B' and hit 'Enter' button after focusing the TextField on 'Scene B', event handler for the default button on 'Scene A' is executed. 现在当我将'场景A'切换到'场景B'并在将TextField聚焦到'场景B'后点击'输入'按钮时,执行“场景A”上默认按钮的事件处理程序。

I have also tried the following variation but I am still facing the same issue. 我也尝试了以下变化,但我仍然面临同样的问题。

    private void loadCentreScene(String fxmlPath){
    try {
        FXMLLoader loader = new FXMLLoader(Admin.class.getResource(fxmlPath));
        AnchorPane page = (AnchorPane) loader.load();
        Plugin fxController = loader.getController();
        fxController.setMainController(this);
        Node node = getRootLayout().getCenter();
        getRootLayout().getChildren().remove(node); //<****Remove the node from children****>
        getRootLayout().setCenter(null); //<****Set center to null****>
        node = null;            
        getRootLayout().setCenter(page);            
    } catch (IOException ex) {
        Dialogs.showErrorDialog(primaryStage, ex.getMessage(), "Loading Error");
    }
}

As per my understanding the object should be unreachable and later on should b garbage collected. 根据我的理解,该对象应该是无法访问的,以后应该收集垃圾。 Can anybody help me to understand why object of 'Scene A' is still reachable and why the event handler is being invoked for the default button. 任何人都可以帮助我理解为什么“场景A”的对象仍然可以访问以及为什么要为默认按钮调用事件处理程序。

This is a known bug : the button should not receive events when it is not part of a Scene. 这是一个已知的错误 :当按钮不是场景的一部分时,按钮不应该接收事件。 The bug is fixed in Java 8. I threw together a quick example of this, and can confirm both the bug in Java 7 and that it is fixed in Java 8. You might want to run your code in Java 8 and see if it works correctly there. 这个错误在Java 8中得到了修复。我把它的一个快速示例汇总在一起,并且可以确认Java 7中的错误并且它已在Java 8中修复。您可能希望在Java 8中运行代码并查看它是否有效正确的。

For a Java 7 workaround, wrap the code in the handler for your defaultButton (and maybe cancel button as well) in 对于Java 7的变通方法,请将代码包装在defaultButton的处理程序中(也可能是取消按钮)

if (button.getScene() != null) {
  //...
}

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

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