Home:ALL Converter>Java FX, switching scenes messes up node alignment of second scene?

Java FX, switching scenes messes up node alignment of second scene?

Ask Time:2017-11-17T13:46:27         Author:JOhAnn4187

Json Formatter

Im working on a java FX application were I switch between my start up scene, startScreen() and my game play scene, create(). I have looked at multiple tutorials on how to switch scenes to make sure I had the right idea, and it seems simple enough.

However, if I start the application with the create() scene the format and alignment of all the nodes within are fine. If I try to start with the startScreen() scene and then switch to the create() using the action event, the alignment of the nodes in the game play scene gets messed up (the boards shift and the labels are placed on top of the boards). As far as I've seen in tutorials, switching scenes shouldn't affect the alignment of nodes of scenes assuming the scenes normally work separately. Original (just calling create()) and Second Version calling both startScreen() and create()

public Parent create() {
    BorderPane root = new BorderPane();
    root.setPrefSize(800, 800);
    setButtons(root);
    enemy = new Board(event -> {...}, true);
    player = new Board(event -> {...}, false);
    VBox vbox = new VBox(50, enemyLabel, enemy, playerLabel, player);
    vbox.setAlignment(Pos.CENTER);
    root.setCenter(vbox);
    return root;
}

public Parent startScreen(Stage primaryStage) {
    BorderPane pane = new BorderPane();
    pane.setPrefSize(800, 800);
    pane.setId("pane");
    Button button = new Button("START");
    button.setOnAction(e-> primaryStage.setScene(new Scene(create())));
    pane.setCenter(button);
    return pane;
}

@Override
public void start(Stage primaryStage) {
    try {
        primaryStage.setTitle("Boat Wars");
        primaryStage.setResizable(false);
        //Scene scene = new Scene(startScreen(primaryStage));
        //scene.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm());
        Scene scene = new Scene(create());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Author:JOhAnn4187,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47344041/java-fx-switching-scenes-messes-up-node-alignment-of-second-scene
yy