Ddz
Ddz

Reputation: 9

JavaFX : Getting back to main page without FXML

I am learning JavaFx on my own and have not reached FXML yet. I am stuck at one application where I plan to have it go back to the main scene of the Application after the user enters their credentials on a second scene. I managed to bring up the second scene from the main one but I could not get from the second scene to the main one. I tried getting the main scene and pane using a getter but no luck. Can you guys teach the right way?

Thank you in advance.

public class Landing extends Application {
    BorderPane bp;
    Scene scene;
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Welcome to our Telco!");
        bp = new BorderPane();
        VBox vbox = new VBox();
        Button login = new Button("Login");
        login.setMinWidth(100);

        Button acc = new Button("Account Information");
        acc.setMinWidth(100);

        vbox.getChildren().addAll(acc);

        bp.setCenter(vbox);

        acc.setOnAction(e ->{
            AccountInfo account = new AccountInfo();
            primaryStage.setTitle("Account Information"); // Set the stage title
            primaryStage.getScene().setRoot(account.getbp());; // Place the scene in the stage      
        });

        scene = new Scene(bp, 750, 550);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public Pane getbp() {
        return bp;
    }
    public Scene getSc(){
        return scene;
    }

the button to get the main scene

public class AccountInfo {
    BorderPane pane;
    Landing main = new Landing();
    Scene scene;
    AccountInfo() {   
        Button c = (new Button("Back"));
        c.setStyle("-fx-background-color: pink");
        c.setOnAction((ActionEvent e) -> {
        main.getbp();
        main.getSc();
    });
    public Pane getbp() {
        return pane;
    }
}

Upvotes: 0

Views: 1018

Answers (1)

Jai
Jai

Reputation: 8363

Landing is not a scene, it is an Application. So far from what you've shown, there is only one scene in your whole application. You must never try to instantiate (and subsequently run) more than one instance of any Application class within the same JavaFX application lifetime. You are dangerously going towards this direction when you do Landing main = new Landing(); in your AccountInfo class.

From Javadoc for Application.launch:

Throws: IllegalStateException - if this method is called more than once.

What you need is to have the first scene for login (i.e. enter credentials). When login is successful, you create a new scene object and populate that scene with your next "view", then set that new scene to the stage.

public class Landing extends Application {
    BorderPane bp;
    Scene scene;

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Welcome to our Telco!");
        bp = new BorderPane();
        VBox vbox = new VBox();
        Button login = new Button("Login");
        login.setMinWidth(100);

        Button acc = new Button("Account Information");
        acc.setMinWidth(100);

        vbox.getChildren().addAll(acc);

        bp.setCenter(vbox);

        acc.setOnAction(e -> {
            primaryStage.setTitle("Account Information"); // Set the stage title
            BorderPane infoScenePane = new BorderPane();
            Scene infoScene = new Scene(infoScenePane);
            primaryStage.setScene(infoScene);
        });

        scene = new Scene(bp, 750, 550);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Upvotes: 1

Related Questions