简体   繁体   中英

JavaFX: how to change a scene included in the center of a BorderPane

I want to change the scene located in the center of the borderpane in my main application when I click the hyperlink. So I coded my program as follows

private BorderPane borderPane;
    private AnchorPane connectionPage;
    @FXML
    private Hyperlink hyperLink1; 

    @FXML
    private void handleHyperLink1OnAction () {
        try {
            connectionPage=FXMLLoader.load(getClass().getResource("ConnectionViewer.fxml"));
        } catch (Exception e) {
            System.out.println(e.getStackTrace().toString());
            System.out.println(e.getMessage());
        }
        borderPane.setCenter(connectionPage);
    }

this is my main application

public void start(Stage primaryStage) throws Exception {
        Parent welcomePage = FXMLLoader.load(getClass().getResource("ManagerWorldViewer.fxml"));

        Scene scene = new Scene(welcomePage);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

Once I run this program, there is nothing changed and no error occurs. Please help me

I missed @FXML annotation. So I changed my source code to..

@FXML
    private BorderPane borderPane;
@FXML
    private AnchorPane anchorPane;

private void changeScreenOfCenter(String path, VBox menuVBox) {
        VBox getVbox = menuVBox;
        try {
            anchorPane = FXMLLoader.load(getClass().getResource(path));
        } catch (Exception e) {
            System.out.println(e.getStackTrace().toString());
            System.out.println(e.getMessage());
        }
}

and it works.

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