Login Application with 1 stage and multiple scene in JavaFX

35,065

Each FXML file is not necessarily a new Scene.

A fxml is just a view file with its root element as any of the Layouts provided by Javafx. It may have multiple Layouts(as a part of the root layout) and controls depending on your requirement.

To know more about fxml, you can view

Java vs JavaFX Script vs FXML. Which is better way of programming in JavaFX?

Tutorial on FXML

http://docs.oracle.com/javafx/2/fxml_get_started/jfxpub-fxml_get_started.htm

Now, once your FXML is ready, you can load it in different ways :

  1. Load as a root of your scene
  2. Load as a part of the already loaded LAYOUT
  3. Load as the root of the new Scene and assign it to the current stage

To help you understand the above points here is an example for each of them. Here, I am demonstrating a LoginController class which is a Controller for loading the FXML.

Example - 1

FXMLLoader loader = new FXMLLoader(LoginController.class.getResource("root.fxml"));
AnchorPane login = (AnchorPane) loader.load();
Scene scene = new Scene(login); 

Example - 2

FXMLLoader loader = new FXMLLoader(LoginController.class.getResource("root.fxml"));
AnchorPane login = (AnchorPane) loader.load();
BorderPane borderPane = (BorderPane)scene.getRoot();
borderPane.setCenter(login);

Example - 3

FXMLLoader loader = new FXMLLoader(LoginController.class.getResource("root.fxml"));
AnchorPane login = (AnchorPane) loader.load();
Scene scene = new Scene(login);
stage.setScene(scene);//Stage loads the new scene, which has the layout of the fxml

N.B. For more details on how to access Stage/Scene on different controllers please go through

https://community.oracle.com/message/11251866

Share:
35,065
jabbeboy
Author by

jabbeboy

Hobby java programmer, and student!

Updated on July 14, 2022

Comments

  • jabbeboy
    jabbeboy almost 2 years

    I am doing a timeline project. I have successfully created a login system and menus for everything, but when I press the buttons I have done so it will open a new window(with stage, scenes). I have read that it isn't the best approach. The best way would be to only have 1 primary stage, and that one would be when I launch the application, the login.

    But I have looked for information about multiple scenes with one stage but I have not found any good solutions. Would really really appreciate some help ;) Hopefully you understand what I want to achieve. Worth mentioning, i=I'm dealing with Scenebuilder and fxml files so I all I want to basically do is load a new .fxml scene onto the primary stage.

    So I have looked in another thread and try to do a VistaFramework that handles all of the scene changes. But I don't understand it fully, and I cant get it to work.

    package application;
    
    import javafx.fxml.FXMLLoader;
    
    import java.io.IOException;
    
    import controllers.MainController;
    
    /**
     * Utility class for controlling navigation between vistas.
     *
     * All methods on the navigator are static to facilitate
     * simple access from anywhere in the application.
     */
    public class VistaNavigator {
    
        /**
         * Convenience constants for fxml layouts managed by the navigator.
         */
        public static final String MAIN    = "LoginGUI.fxml";
        public static final String NEW_USER = "NewUserGUI.fxml";
        public static final String STARTMENU = "StartMenuGUI.fxml";
    
        /** The main application layout controller. */
        private static MainController mainController;
    
        /**
         * Stores the main controller for later use in navigation tasks.
         *
         * @param mainController the main application layout controller.
         */
        public static void setMainController(MainController mainController) {
            VistaNavigator.mainController = mainController;
        }
    
        /**
         * Loads the vista specified by the fxml file into the
         * vistaHolder pane of the main application layout.
         *
         * Previously loaded vista for the same fxml file are not cached.
         * The fxml is loaded anew and a new vista node hierarchy generated
         * every time this method is invoked.
         * @param fxml the fxml file to be loaded.
         */
    
        public static void loadVista(String fxml) {
            try {
                mainController.setVista(
                    FXMLLoader.load(
                        VistaNavigator.class.getResource(
                            fxml
                        )
                    )
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    I get a error in loadVista(). Get the following error at mainController.setVista( "The method setVista(Node) in the type MainController is not applicable for the arguments (Object)"