title: Problem with setting the value of FXML Objects and implementing FXML Action Events at the same time
body: <p>I've got a problem considering the loading of the FXML objects and the creating of the action events:
  This is my code:</p>
  <p>I open up the problematic scene in another class with following code:</p>
  <p>Controller 1:</p>
  <pre><code>        package sample;
  
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Node;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.stage.Stage;
  
  import java.io.IOException;
  
  public class Controller {
  
      private Stage window;
      public Button profilScene;
  
  
      public void profilSwitch(ActionEvent event) throws IOException {
          window = (Stage)((Node)event.getSource()).getScene().getWindow();
          Controller2 controllerProfiluebersicht = new Controller2();
  
          FXMLLoader loaderProfil = new FXMLLoader(getClass().getResource(&quot;/sample/profilübersicht.fxml&quot;));
          loaderProfil.setController(controllerProfiluebersicht);
  
          Parent profilScene = loaderProfil.load();
          controllerProfiluebersicht.label();
  
  
          Scene profil = new Scene(profilScene);
  
  
  
          window.setScene(profil);
      }
      }
  </code></pre>
  <p>Problematic scene:
  Controller 2:</p>
  <pre><code>        package sample;
  
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.stage.Stage;
  
  public class Controller2 {
  
      FXMLLoader loaderProfil = new FXMLLoader(getClass().getResource(&quot;/sample/profilübersicht.fxml&quot;));
  
  
  
  
      private Stage window;
      //-----------------------------set label ---------------------------------------
  
  
      public void label (){
  
          profilMiId.setText(&quot;example 1&quot;);
          profilEmail.setText(&quot;example 2&quot;);
          profilPasswort.setText(&quot;example 3&quot;);
  
      }
  
      @FXML
      private Label profilDatum;
  
      @FXML
      private Label profilEmail;
  
      @FXML
      private Label profilMiId;
  
      @FXML
      private Label profilPasswort;
  
  
      //----------------------------------------------------------------------------------
  
      @FXML
      private Button profilKnopfRaum;
  //this button imported from the fxml file needs a action event method
  
  }
  </code></pre>
  <p>FXML Controller 1:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/8.0.171&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;sample.Controller&quot;&gt;
     &lt;children&gt;
        &lt;Button fx:id=&quot;profilScene&quot; layoutX=&quot;272.0&quot; layoutY=&quot;188.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#profilSwitch&quot; text=&quot;scene 2&quot; /&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  <p>FXML Controller 2:</p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.Cursor?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.ChoiceBox?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  &lt;?import javafx.scene.effect.DropShadow?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.layout.BorderPane?&gt;
  &lt;?import javafx.scene.layout.ColumnConstraints?&gt;
  &lt;?import javafx.scene.layout.GridPane?&gt;
  &lt;?import javafx.scene.layout.Pane?&gt;
  &lt;?import javafx.scene.layout.RowConstraints?&gt;
  &lt;?import javafx.scene.layout.VBox?&gt;
  &lt;?import javafx.scene.text.Font?&gt;
  
  &lt;AnchorPane maxHeight=&quot;750.0&quot; maxWidth=&quot;1295.0&quot; minHeight=&quot;750.0&quot; minWidth=&quot;1295.0&quot; prefHeight=&quot;750.0&quot; prefWidth=&quot;1295.0&quot; xmlns=&quot;http://javafx.com/javafx/8.0.171&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
     &lt;children&gt;
        &lt;VBox prefHeight=&quot;750.0&quot; prefWidth=&quot;378.0&quot; style=&quot;-fx-background-color: #E20074;&quot; AnchorPane.bottomAnchor=&quot;0.0&quot; AnchorPane.topAnchor=&quot;0.0&quot;&gt;
           &lt;children&gt;
              &lt;Pane prefHeight=&quot;241.0&quot; prefWidth=&quot;378.0&quot;&gt;
                 &lt;children&gt;
                    &lt;Label layoutX=&quot;102.0&quot; layoutY=&quot;63.0&quot; textFill=&quot;WHITE&quot;&gt;
                       &lt;font&gt;
                          &lt;Font name=&quot;TeleLogo&quot; size=&quot;45.0&quot; /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                    &lt;Label layoutX=&quot;55.0&quot; layoutY=&quot;128.0&quot; prefHeight=&quot;23.0&quot; prefWidth=&quot;269.0&quot; textFill=&quot;WHITE&quot;&gt;
                       &lt;font&gt;
                          &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;19.0&quot; /&gt;
                       &lt;/font&gt;
                    &lt;/Label&gt;
                 &lt;/children&gt;
              &lt;/Pane&gt;
              &lt;Button fx:id=&quot;profilKnopfRaum&quot; mnemonicParsing=&quot;false&quot; prefHeight=&quot;93.0&quot; prefWidth=&quot;416.0&quot; style=&quot;-fx-background-color: #ca1f7b;&quot; text=&quot;Übersicht der Räume&quot; textFill=&quot;WHITE&quot;&gt;
                 &lt;font&gt;
                    &lt;Font name=&quot;Tele-GroteskFet&quot; size=&quot;30.0&quot; /&gt;
                 &lt;/font&gt;
                 &lt;cursor&gt;
                    &lt;Cursor fx:constant=&quot;HAND&quot; /&gt;
                 &lt;/cursor&gt;
              &lt;/Button&gt;
              &lt;Pane prefHeight=&quot;49.0&quot; prefWidth=&quot;378.0&quot; /&gt;
              &lt;Button fx:id=&quot;profilKnopfReservieren&quot; mnemonicParsing=&quot;false&quot; prefHeight=&quot;93.0&quot; prefWidth=&quot;378.0&quot; style=&quot;-fx-background-color: #ca1f7b;&quot; text=&quot;Raum reservieren&quot; textFill=&quot;WHITE&quot;&gt;
                 &lt;font&gt;
                    &lt;Font name=&quot;Tele-GroteskFet&quot; size=&quot;30.0&quot; /&gt;
                 &lt;/font&gt;
                 &lt;cursor&gt;
                    &lt;Cursor fx:constant=&quot;HAND&quot; /&gt;
                 &lt;/cursor&gt;
              &lt;/Button&gt;
              &lt;Pane prefHeight=&quot;49.0&quot; prefWidth=&quot;378.0&quot; /&gt;
              &lt;Button mnemonicParsing=&quot;false&quot; prefHeight=&quot;93.0&quot; prefWidth=&quot;378.0&quot; style=&quot;-fx-background-color: #ca1f7b;&quot; text=&quot;Profil&quot; textFill=&quot;WHITE&quot;&gt;
                 &lt;font&gt;
                    &lt;Font name=&quot;Tele-GroteskFet&quot; size=&quot;30.0&quot; /&gt;
                 &lt;/font&gt;
                 &lt;cursor&gt;
                    &lt;Cursor fx:constant=&quot;HAND&quot; /&gt;
                 &lt;/cursor&gt;
                 &lt;effect&gt;
                    &lt;DropShadow height=&quot;255.0&quot; radius=&quot;63.25&quot; width=&quot;0.0&quot; /&gt;
                 &lt;/effect&gt;
              &lt;/Button&gt;
           &lt;/children&gt;
        &lt;/VBox&gt;
        &lt;BorderPane layoutX=&quot;378.0&quot; prefHeight=&quot;750.0&quot; prefWidth=&quot;918.0&quot;&gt;
           &lt;top&gt;
              &lt;VBox prefHeight=&quot;174.0&quot; prefWidth=&quot;927.0&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
                 &lt;children&gt;
                    &lt;Pane prefHeight=&quot;94.0&quot; prefWidth=&quot;917.0&quot;&gt;
                       &lt;children&gt;
                          &lt;Label fx:id=&quot;profilDatum&quot; layoutX=&quot;52.0&quot; layoutY=&quot;32.0&quot; prefHeight=&quot;23.0&quot; prefWidth=&quot;105.0&quot; text=&quot;01.01.2021&quot;&gt;
                             &lt;font&gt;
                                &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;19.0&quot; /&gt;
                             &lt;/font&gt;
                          &lt;/Label&gt;
                       &lt;/children&gt;&lt;/Pane&gt;
                    &lt;Pane prefHeight=&quot;83.0&quot; prefWidth=&quot;918.0&quot; style=&quot;-fx-background-color: #dbdbdb;&quot;&gt;
                       &lt;children&gt;
                          &lt;Label layoutX=&quot;424.0&quot; layoutY=&quot;22.0&quot; text=&quot;Profil&quot;&gt;
                             &lt;font&gt;
                                &lt;Font name=&quot;TeleNeo Office Bold&quot; size=&quot;32.0&quot; /&gt;
                             &lt;/font&gt;
                          &lt;/Label&gt;
                       &lt;/children&gt;
                    &lt;/Pane&gt;
                 &lt;/children&gt;
              &lt;/VBox&gt;
           &lt;/top&gt;
           &lt;center&gt;
              &lt;Pane prefHeight=&quot;560.0&quot; prefWidth=&quot;918.0&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
                 &lt;children&gt;
                    &lt;VBox prefHeight=&quot;576.0&quot; prefWidth=&quot;918.0&quot;&gt;
                       &lt;children&gt;
                          &lt;Pane prefHeight=&quot;66.0&quot; prefWidth=&quot;918.0&quot; /&gt;
                          &lt;Pane prefHeight=&quot;35.0&quot; prefWidth=&quot;918.0&quot; style=&quot;-fx-background-color: #dbdbdb;&quot; /&gt;
                          &lt;GridPane prefHeight=&quot;481.0&quot; prefWidth=&quot;918.0&quot;&gt;
                            &lt;columnConstraints&gt;
                              &lt;ColumnConstraints hgrow=&quot;SOMETIMES&quot; minWidth=&quot;10.0&quot; prefWidth=&quot;100.0&quot; /&gt;
                              &lt;ColumnConstraints hgrow=&quot;SOMETIMES&quot; minWidth=&quot;10.0&quot; prefWidth=&quot;100.0&quot; /&gt;
                            &lt;/columnConstraints&gt;
                            &lt;rowConstraints&gt;
                              &lt;RowConstraints minHeight=&quot;10.0&quot; prefHeight=&quot;30.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
                              &lt;RowConstraints maxHeight=&quot;155.0&quot; minHeight=&quot;10.0&quot; prefHeight=&quot;122.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
                              &lt;RowConstraints maxHeight=&quot;197.0&quot; minHeight=&quot;10.0&quot; prefHeight=&quot;197.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
                            &lt;/rowConstraints&gt;
                             &lt;children&gt;
                                &lt;Pane prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot;&gt;
                                   &lt;children&gt;
                                      &lt;Label alignment=&quot;CENTER_RIGHT&quot; layoutX=&quot;175.0&quot; layoutY=&quot;51.0&quot; prefHeight=&quot;62.0&quot; prefWidth=&quot;193.0&quot; text=&quot;E-Mail Adresse:&quot;&gt;
                                         &lt;font&gt;
                                            &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;22.0&quot; /&gt;
                                         &lt;/font&gt;
                                      &lt;/Label&gt;
                                   &lt;/children&gt;
                                &lt;/Pane&gt;
                                &lt;Pane prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; GridPane.rowIndex=&quot;1&quot;&gt;
                                   &lt;children&gt;
                                      &lt;Label alignment=&quot;CENTER_RIGHT&quot; layoutX=&quot;176.0&quot; layoutY=&quot;7.0&quot; prefHeight=&quot;62.0&quot; prefWidth=&quot;193.0&quot; text=&quot;Mitarbeiter-ID:&quot;&gt;
                                         &lt;font&gt;
                                            &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;22.0&quot; /&gt;
                                         &lt;/font&gt;
                                      &lt;/Label&gt;
                                      &lt;Label alignment=&quot;CENTER_RIGHT&quot; layoutX=&quot;176.0&quot; layoutY=&quot;93.0&quot; prefHeight=&quot;62.0&quot; prefWidth=&quot;193.0&quot; text=&quot;Passwort:&quot;&gt;
                                         &lt;font&gt;
                                            &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;22.0&quot; /&gt;
                                         &lt;/font&gt;
                                      &lt;/Label&gt;
                                   &lt;/children&gt;
                                &lt;/Pane&gt;
                                &lt;Pane prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; GridPane.rowIndex=&quot;2&quot;&gt;
                                   &lt;children&gt;
                                      &lt;Label alignment=&quot;CENTER_RIGHT&quot; layoutX=&quot;174.0&quot; layoutY=&quot;68.0&quot; prefHeight=&quot;62.0&quot; prefWidth=&quot;193.0&quot; text=&quot;Reservierungen:&quot;&gt;
                                         &lt;font&gt;
                                            &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;22.0&quot; /&gt;
                                         &lt;/font&gt;
                                      &lt;/Label&gt;
                                   &lt;/children&gt;
                                &lt;/Pane&gt;
                                &lt;Pane prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; GridPane.columnIndex=&quot;1&quot;&gt;
                                   &lt;children&gt;
                                      &lt;Label fx:id=&quot;profilEmail&quot; alignment=&quot;CENTER&quot; layoutY=&quot;51.0&quot; prefHeight=&quot;62.0&quot; prefWidth=&quot;302.0&quot; text=&quot;Hier ist die E-Mail Adresse&quot;&gt;
                                         &lt;font&gt;
                                            &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;22.0&quot; /&gt;
                                         &lt;/font&gt;
                                      &lt;/Label&gt;
                                   &lt;/children&gt;
                                &lt;/Pane&gt;
                                &lt;Pane prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; GridPane.columnIndex=&quot;1&quot; GridPane.rowIndex=&quot;1&quot;&gt;
                                   &lt;children&gt;
                                      &lt;Label fx:id=&quot;profilMiId&quot; alignment=&quot;CENTER&quot; layoutY=&quot;8.0&quot; prefHeight=&quot;62.0&quot; prefWidth=&quot;302.0&quot; text=&quot;Hier ist die Mitarbeiter ID&quot;&gt;
                                         &lt;font&gt;
                                            &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;22.0&quot; /&gt;
                                         &lt;/font&gt;
                                      &lt;/Label&gt;
                                      &lt;Label fx:id=&quot;profilPasswort&quot; alignment=&quot;CENTER&quot; layoutY=&quot;93.0&quot; prefHeight=&quot;62.0&quot; prefWidth=&quot;302.0&quot; text=&quot;Hier ist das Passwort&quot;&gt;
                                         &lt;font&gt;
                                            &lt;Font name=&quot;TeleNeo Office Regular&quot; size=&quot;22.0&quot; /&gt;
                                         &lt;/font&gt;
                                      &lt;/Label&gt;
                                   &lt;/children&gt;
                                &lt;/Pane&gt;
                                &lt;Pane prefHeight=&quot;200.0&quot; prefWidth=&quot;200.0&quot; GridPane.columnIndex=&quot;1&quot; GridPane.rowIndex=&quot;2&quot;&gt;
                                   &lt;children&gt;
                                      &lt;ChoiceBox fx:id=&quot;reservierungenAusklapp&quot; layoutX=&quot;74.0&quot; layoutY=&quot;83.0&quot; prefHeight=&quot;32.0&quot; prefWidth=&quot;182.0&quot; /&gt;
                                   &lt;/children&gt;
                                &lt;/Pane&gt;
                             &lt;/children&gt;
                          &lt;/GridPane&gt;
                       &lt;/children&gt;
                    &lt;/VBox&gt;
                 &lt;/children&gt;
              &lt;/Pane&gt;
           &lt;/center&gt;
        &lt;/BorderPane&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  <p><strong>So the issue is following:</strong></p>
  <p>With this given code I can set a value to the FMXL labels as you can see in Controller 2, method: &quot;label()&quot;. It is only possible because of declaring the controller of the problematic scene only in the this first controller with: &quot;loaderProfil.setController(controllerProfiluebersicht)&quot;(it is not declared in the fxml file) and then loading the FXML file.
  But if I do that i cant use action events of the FXML buttons on the second scene because the FXML file wants me to implement these methods in its controller which is not given in the fxml.</p>
  <p>On the other hand if I declare it on the FXML file I am not able to set a value to the FXML labels because they are &quot;null&quot;.</p>
  <p>I'm looking for a solution where I am able to set values to the FXML objects as well as using theri on action methods.</p>
  <p>I appreciate any kind of help :)</p>