JavaFX- Return to previous scene with seperate classes

2020-04-01 java javafx

I made a very simple GUI with some buttons and some styling. On 2 of the 4 Scenes there is a "Go Back" button which as it says is supposed to bring you back to the previous scene. Normally when the scene is set within the start method you can just do

stage.setScene(scene);

But I don't have the scene set within the main start method. Instead I have my scenes distributed between different classes like this:

Main

public class Main extends Application {

    Stage window;
    Scene mainScene;

    @Override
    public void start(Stage stage) throws Exception {
        window = stage;
        window.setTitle("Main screen");
        MainView mainView = new MainView(window);

        window.setOnCloseRequest(e -> {
            e.consume();
            mainView.closeApplication(window);
        });

        mainScene = new Scene(mainView, 900, 600);
        window.setScene(mainScene);
        window.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

MainView

public class MainView extends VBox {
    public MainView(Stage window) {
        setSpacing(10);
        setPadding(new Insets(20,20,20,20));
        setAlignment(Pos.CENTER);
        setStyle("-fx-background-color: #1da1f2;");

        Label title = new Label("Project 2.3 SE");
        title.setTextFill(Color.WHITE);
        title.setFont(new Font("Arial", 30));

        Button chooseGame = new Button("Play");
        Button options = new Button("Options");
        Button close = new Button("Exit");

        chooseGame.setMinWidth(100);
        options.setMinWidth(100);
        close.setMinWidth(100);

        // Wanneer men een game kiest veranderd de scene naar een nieuwe scene
        ChooseGameView chooseGameView = new ChooseGameView(window);
        Scene chooseGameScene = new Scene(chooseGameView, 900, 600);

        chooseGame.setOnAction(e -> {
            window.setScene(chooseGameScene);
            window.setTitle("Choose a game");
        });

        close.setOnAction(e -> {
            closeApplication(window);
        });

        getChildren().addAll(title, chooseGame, options, close);
    }
}

GameView

public class ChooseGameView extends VBox {

    public ChooseGameView(Stage window) {
        setSpacing(10);
        setPadding(new Insets(20,20,20,20));
        setAlignment(Pos.CENTER);
        setStyle("-fx-background-color: #1da1f2;");

        Label chooseGameTitle = new Label("Choose a game to play");
        chooseGameTitle.setTextFill(Color.WHITE);
        chooseGameTitle.setFont(new Font("Arial", 30));
        Button chooseTTT = new Button("Tic Tac Toe");
        Button reversi = new Button("Reversi");
        Button back = new Button("Go back");

        chooseTTT.setMinWidth(100);
        reversi.setMinWidth(100);
        back.setMinWidth(100);

        ChooseGameModeView chooseGameModeView = new ChooseGameModeView(window);
        Scene chooseGameModeViewScene = new Scene(chooseGameModeView, 900, 600);

        chooseTTT.setOnAction(e -> {
            window.setScene(chooseGameModeViewScene);
            window.setTitle("Tic Tac Toe");
        });

        back.setOnAction(e -> {
            System.out.println("Moet terug naar MainView");
        });

        getChildren().addAll(chooseGameTitle, chooseTTT, reversi, back);
    }
}

Within the GameView I have a button called "Back" which is supposed to bring you back to the MainView. I dont really know how to do that? I tried something like

MainView mainView = new MainView(window);
        Scene scene = new Scene(mainView)

        back.setOnAction(e -> {
            window.setScene(scene);
        });

But that does not work and crashes the program. How can I solve this?

Answers

Related