Search code examples
javajavafxbuilder

How To Add New fxml to Controller


I have 2 .fxml files (FXMLDocument.fxml/login.fxml) and one controller file (FXMLDocumentController). I want to remove FXMLDocument.fxml from the controller and add login.fxml to the contoller.

When I call the main method, I want get prompted with the login form instead of FXMLDocument.fxml.

Any idea what is happening?

PS: login.fxml is created using SceneBuilder.


Solution

  • You don't have to use the default FXML file generated by your IDE. Here is a basic controller that will use login.fxml:

    public class LoginController {
    
        public LoginController(){
            try {
                FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("login.fxml"));
                fxmlLoader.setController(this);
                fxmlLoader.load();
            } catch (IOException exception) {
                System.out.println("Error loading content.");
                System.exit(0);
            }
        }
    
    }
    

    Note the line with FXMLLoader(getClass().getResource("login.fxml")). This is how you add an FXML file to a controller.