1

I am currently studying Java and my university has asked me to create a small JavaFX app. However, I am having an issue with one of the buttons I set - it is not displaying on certain scenes, while the rest of them are working correctly.

I have been practicing how to switch between scenes and have split my code into three sections for better understanding in the future. The first section contains labels, the second one sets the buttons, and the last one sets the layouts and scenes.

The button in question is "buttonMain", which is supposed to allow me to go back to the main scene. However, it is not displaying on the following scenes: "sceneRegMascota", "sceneRegUsuario", "sceneInicioSesion", and "sceneListMascotas". This issue is causing me to get stuck when switching to any of these scenes.

public class MainApp extends Application{

    Stage window;
    Scene sceneMain, sceneRegMascota, sceneRegUsuario, sceneInicioSesion,sceneListMascotas;
    public static void main(String[] args) {
        launch(args);
    }

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



        //sceneMain + labels
        Label labelMain =new Label("Bienvenido a Veterinaria Moka");
        Label labelRegUsuario =new Label("Registro de Usuario");
        Label labelInicioSesion =new Label("Inicio de sesion");
        Label labelRegMascota =new Label("Registro de Nueva Mascota");
        Label labelListMascotas =new Label("Listar Mascotas");




        //sceneMain + buttons
        Button buttonMain = new Button("Volver al Menu Principal");
        buttonMain.setOnAction(e -> window.setScene(sceneMain));

        Button buttonRegUsuario = new Button("Registro de Nuevo Usuario");
        buttonRegUsuario.setOnAction(e -> window.setScene(sceneRegUsuario));

        Button buttonInicioSesion = new Button("Inicio de Sesion");
        buttonInicioSesion.setOnAction(e -> window.setScene(sceneInicioSesion));

        Button buttonRegMascota = new Button("Registrar nueva Mascota");
        buttonRegMascota.setOnAction(e -> window.setScene(sceneRegMascota));

        Button buttonListMascotas = new Button("Lista de Mascotas");
        buttonListMascotas.setOnAction(e -> window.setScene(sceneListMascotas));

        

        //sceneMain + Layouts
        VBox layoutMain = new VBox(20);
        layoutMain.getChildren().addAll(labelMain, buttonRegUsuario, buttonInicioSesion, buttonRegMascota, buttonListMascotas);
        sceneMain = new Scene(layoutMain, 400, 500);

        VBox layoutRegUsuario = new VBox(20);
        layoutRegUsuario.getChildren().addAll(labelRegUsuario, buttonMain);
        sceneRegUsuario = new Scene(layoutRegUsuario, 200, 200);

        VBox layoutInicioSesion = new VBox(20);
        layoutInicioSesion.getChildren().addAll(labelInicioSesion, buttonMain);
        sceneInicioSesion = new Scene(layoutInicioSesion, 200, 200);

        VBox layoutRegMascota = new VBox(20);
        layoutRegMascota.getChildren().addAll(labelRegMascota, buttonMain);
        sceneRegMascota = new Scene(layoutRegMascota, 200, 200);

        VBox layoutListMascotas = new VBox(20);
        layoutListMascotas.getChildren().addAll(labelListMascotas, buttonMain);
        sceneListMascotas = new Scene(layoutListMascotas, 200, 200);

        window.setScene(sceneMain);
        window.setTitle("Veterinaria Moka Admin");
        window.show();
    }
}

Expected Results:

When switching to any of the scenes ("sceneRegMascota", "sceneRegUsuario", "sceneInicioSesion", or "sceneListMascotas"), the "buttonMain" should be displayed so that I can go back to the main scene when I want to.

Actual Results:

Unfortunately, when switching to any of the above scenes, the "buttonMain" is not displayed, and I cannot go back to the main scene. This is causing me to get stuck and unable to navigate the app as intended. sceneInicioSesion with no buttons

  • 1
    Any node can only appear in one scene. – James_D Apr 06 '23 at 19:39
  • 1
    Don't switch out the entire scene. Instead, construct a scene of nested panes, with your navigation buttons in a different position in the scene graph (e.g. in the top panel of a border pane) and the main content in a separate child pane (e.g. the middle panel of a border pane) which you can swap out for something else. – jewelsea Apr 06 '23 at 19:50
  • 1
    The child pane swap approach is demonstrated by [this answer](https://stackoverflow.com/a/18651659/1155209) which uses [this code](https://gist.github.com/jewelsea/6460130). That code is for FXML-based content, but you can generate the nodes in Java instead of FXML if desired. There will be simpler examples of the concept in some other similar questions on StackOverflow. – jewelsea Apr 06 '23 at 19:52

0 Answers0