Login Application with Best Answertage and multiple scene in JavaFX

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

Leave a Comment