简体   繁体   中英

JavaFX: Remove the scene loaded in Center of BorderPane

I have a JavaFX2.2 application which has a BorderPane on the main screen. 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.

'Scene A' has two buttons. One of them defined as 'Default Button' and other as 'Cancel Button' in the FXML file.

'Scene B' has a TextField and a 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.

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.

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.

For a Java 7 workaround, wrap the code in the handler for your defaultButton (and maybe cancel button as well) in

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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