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