Jason Per
Jason Per

Reputation: 149

JavaFX switching scenes

I've been trying to make my application to switch between scenes. Here is a copy of part of the code. The credits scene simply has a back button which should return me to the main scene.

When I try to click on credits button on main scene it is becoming white a white screen. I believe that there is a better way to solve this problem could you give me some advices ?

public class Application {
public static void main(String[] args) {
    javafx.application.Application.launch(GUI.class);
}
}


public class GUI extends Application {

@Override
public void start(Stage primaryStage) {
    Scene mainScene, creditsScene = null;
    mainScene = getMainScene(primaryStage, creditsScene);
    creditsScene = getCreditsScene(primaryStage, mainScene);
    primaryStage.setTitle("Test application");
    primaryStage.setScene(mainScene);
    primaryStage.show();
}

private Scene getMainScene(Stage primaryStage, Scene creditsScene) {
 final Button credits = new Button("Credits");
    credits.setOnAction((ActionEvent e) -> {
        primaryStage.close();
        primaryStage.setScene(creditsScene);
        primaryStage.show();
    });
    VBox x = new VBox(50);
    x.setAlignment(Pos.CENTER);

    x.getChildren().addAll( run, displayInfo,
            label1, displayInfo, textField, submitName, credits, exit);

    //scene size
    Scene scene = new Scene(x, 650, 900);

    return scene;
}


 private Scene getCreditsScene(Stage primaryStage, Scene main) {
    final Button back = new Button("Back");
    back.setOnAction((ActionEvent e) -> {
        primaryStage.setScene(main);
    });
    VBox x = new VBox(50);
    x.getChildren().addAll(back);
    Scene credits = new Scene(x, 650, 900);
    return credits;
}

Upvotes: 1

Views: 204

Answers (1)

Michel_T.
Michel_T.

Reputation: 2821

Try to switch order of strings:

mainScene = getMainScene(primaryStage, creditsScene);
creditsScene = getCreditsScene(primaryStage, mainScene);

here you pass to getMainScene null.

Upvotes: 1

Related Questions