0

I'm new to JavaFX and I'm trying to understand the best way to architect my app. My app starts showing a register/log in form and after you do either action, that disappears and gets replaced with the actual UI of the application.

Should this be two scenes or should it be two panes (or something like that) in the same scene?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • 1
    Several approaches are examined in [*Switch between panes in JavaFX*](http://stackoverflow.com/q/16176701/230513). – trashgod May 14 '17 at 12:43
  • The choice is yours. I have an example that loads a different scene after successful login [here](https://github.com/sedj601/SimpleLoginFx). – SedJ601 May 14 '17 at 16:30
  • @SedrickJefferson: thanks for pointing to that example. Unfortunately, it creates separate stages, not exactly what I want. I want to stay within one stage. – Pablo Fernandez May 15 '17 at 05:44

1 Answers1

-1

Using a single Stage (window) and switching between 2 Scenes or using 2 windows switching between them is completely up to you. Here is an example code on how to do it using 1 Stage and switching between Scenes. I need to give the credit for this code to @thenewboston who has a very nice, useful and easy to follow JavaFX tutorial on YouTube:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

public class Main extends Application {
    Stage window;
    Scene scene1, scene2;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primartStage;

        Label label1 = new Label("Welcome to the first scene!");
        Button button1 = new Button("Go to scene 2");
        button1.setAction(e -> window.setScene(scene2));

        //  Layout 1 - children are laid out in vertical column
        VBox layout1 = new VBox(20);

        layout1.getChildren().addAll(label1, button1);
        scene1 = new Scene(layout1, 200, 200);

        //  Button 2
        Button button2 = new Button("Go back to scene 1");
        button2.setAction(e -> window.setScene(scene1));

        //  Layout 2
        StackPane layout2 = new StackPane();
        layout2.getChildren().add(button2);

        scene2 = new Scene(layout2, 600, 600);

        window.setScene(scene1);
        window.setTitle("Switching Scenes");
        window.show();
    }
}
Binyamin Regev
  • 914
  • 5
  • 19
  • 31
  • This seems to answer the question "two stages or two scenes", not the question "two scenes or two [root] panes". – James_D May 14 '17 at 14:09
  • I think it's already common practice to show a separate login and after successful log-in to show the rest of the app. If using 2 panes on the same window (`Stage`) then enable only the login pane at first and after successful log-in enable the other pane. It depends on UI/UX. – Binyamin Regev May 14 '17 at 14:38
  • There is no UX difference between switching the scene within a single stage, or switching the root of a single scene (necessarily in a single stage). I think you have missed the point of the question, which doesn't ever mention two stages as an option. – James_D May 14 '17 at 14:41
  • could be I missed the point, or didn't fully understood the question. – Binyamin Regev May 14 '17 at 14:45
  • Yes, that is correct, I wasn't considering two stages. My question was two scenes vs two panes in one scene. – Pablo Fernandez May 15 '17 at 05:39