title: How to make a proper MVC Pattern with Javafx and Scene builder
body: <p>Hi I'm very new to Java and Javafx so I hope you can help me with a problem. I'm trying to do a proper MVC Pattern with Scene Builder but my code doesn't work and I don't know why.</p>
  
  <p>I have understood that the Model class have to get the data and the Controller class should use and handle the data but I have big problems with that the Scene builder does accept one controller class for one FXML file. That is why I tried it with getters and setters to have the connection between Model and Controller.</p>
  
  <p>But also I think I am not doing it right.</p>
  
  <p>Main class:</p>
  
  <pre><code>package application;
  
  import javafx.application.Application;
  import javafx.fxml.*;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class Main extends Application{
  
      @Override
      public void start(Stage primaryStage) throws Exception{
  
          try {
          Parent root = FXMLLoader.load(getClass().getResource("/login/LoginUI.fxml"));
          Scene scene = new Scene(root, 400, 400);
          primaryStage.setScene(scene);
          primaryStage.show();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void startApp(Stage Stage) throws Exception{
  
          try {
              Parent root = FXMLLoader.load(getClass().getResource("/financeApp/UI.fxml"));
              Scene scene = new Scene(root, 1022, 593);
              Stage.setScene(scene);
              Stage.show();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>Controller class:</p>
  
  <pre><code>package login;
  
  import application.Main;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.scene.control.Button;
  import javafx.scene.control.PasswordField;
  import javafx.scene.control.TextField;
  import javafx.stage.Stage;
  import login.ModelLogin;
  
  public class ControllerLogin {
  
      @FXML TextField userNameField;
      @FXML PasswordField passwordField;
      @FXML Button loginButton;
      ModelLogin model = new ModelLogin();
  
      public void setUserName() {
          model.setUserNameField(userNameField);
      }
  
      public void setPassword() {
          model.setPasswordField(passwordField);
      }
  
      public void login(ActionEvent event) {
          if (model.getUserNameField().getText().equals("test") &amp;&amp; model.getPasswordField().getText().equals("1234")) {
              Stage stage = new Stage();
              Main startUI = new Main();
  
              try {
                  startUI.startApp(stage);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }else {
              System.out.println("Try again");
          }
      }
  }
  </code></pre>
  
  <p>Model class:</p>
  
  <pre><code>package login;
  
  import javafx.scene.control.Button;
  import javafx.scene.control.PasswordField;
  import javafx.scene.control.TextField;
  
  public class ModelLogin {
  
      private TextField userNameField;
      private PasswordField passwordField;
  
      public TextField getUserNameField() {
          return userNameField;
      }
      public void setUserNameField(TextField userNameField) {
          this.userNameField = userNameField;
      }
      public PasswordField getPasswordField() {
          return passwordField;
      }
      public void setPasswordField(PasswordField passwordField) {
          this.passwordField = passwordField;
      }
  
  }
  </code></pre>
  
  <p>and this is the FXML file created with Scene builder:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.text.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="290.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.ControllerLogin"&gt;
     &lt;children&gt;
        &lt;AnchorPane prefHeight="290.0" prefWidth="400.0"&gt;
           &lt;children&gt;
              &lt;Label alignment="CENTER" layoutX="150.0" layoutY="38.0" prefHeight="30.0" prefWidth="100.0" text="Login"&gt;
                 &lt;font&gt;
                    &lt;Font name="System Bold" size="20.0" /&gt;
                 &lt;/font&gt;
              &lt;/Label&gt;
              &lt;Label layoutX="159.0" layoutY="108.0" text="Benutzername"&gt;
                 &lt;font&gt;
                    &lt;Font name="System Bold" size="12.0" /&gt;
                 &lt;/font&gt;
              &lt;/Label&gt;
              &lt;TextField fx:id="userNameField" layoutX="126.0" layoutY="125.0" onAction="#setUserName" /&gt;
              &lt;Label layoutX="175.0" layoutY="165.0" text="Passwort"&gt;
                 &lt;font&gt;
                    &lt;Font name="System Bold" size="12.0" /&gt;
                 &lt;/font&gt;
              &lt;/Label&gt;
              &lt;PasswordField fx:id="passwordField" layoutX="126.0" layoutY="182.0" onAction="#setPassword" /&gt;
              &lt;Button fx:id="loginButton" layoutX="175.0" layoutY="233.0" mnemonicParsing="false" onAction="#login" text="Login" /&gt;
           &lt;/children&gt;
        &lt;/AnchorPane&gt;
     &lt;/children&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p>Folders</p>
  
  <p><a href="https://i.stack.imgur.com/VyRu1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VyRu1.png" alt="enter image description here"></a></p>
  
  <p>I would be happy about some Feedback. Thank you!!</p>