title: Unable to control elements of another fxml file through another controller
body: <p>I am working on a project which deals with multiple fxml and corresponding controller files.  I need to somehow get access to an fxml element defined in say <strong>A.fxml</strong> from the controller of <strong>B.fxml</strong> and use it.</p>
  
  <p>I am not allowed to show the actual code.
  However, for this purpose, I have built a simple application with two FXMLs and their corresponding controllers.</p>
  
  <p>This application has Button.fxml with ButtonController.java and ProgressIndicator.fxml with ProgressIndicatorController.java</p>
  
  <p>Upon clicking the button, the Progress indicator would come live.</p>
  
  <p>Below is the code :</p>
  
  <p><strong>Button.fxml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.layout.Pane?&gt;
  
  &lt;AnchorPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ButtonController"&gt;
     &lt;children&gt;
        &lt;Pane prefHeight="206.0" prefWidth="281.0"&gt;
           &lt;children&gt;
              &lt;Button fx:id="button1" layoutX="59.0" layoutY="63.0" mnemonicParsing="false" prefHeight="63.0" prefWidth="164.0" text="Button" onAction="#onButton1Clicked" /&gt;
           &lt;/children&gt;
        &lt;/Pane&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p><strong>ButtonController.java</strong></p>
  
  <pre><code>package application;
  
  import java.io.IOException;
  import java.net.URL;
  import java.util.ResourceBundle;
  
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.ProgressIndicator;
  import javafx.scene.paint.Color;
  import javafx.stage.Stage;
  
  public class ButtonController implements Initializable{
  
      @FXML
      private Button button1;
  
      private ProgressIndicator progressIndicator;
  
      private ProgressIndicatorController progressIndicatorController;
  
      @Override
      public void initialize(URL location, ResourceBundle resources) {
  
          try {
              Object loader = FXMLLoader.load(getClass().getResource("ProgressIndicator.fxml"));
              Scene sceneProgressIndicator = new Scene((Parent) loader, 1400, 800);
              sceneProgressIndicator.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
              sceneProgressIndicator.setFill(Color.TRANSPARENT);
              Stage primaryStageProgressIndicator = new Stage();
              primaryStageProgressIndicator.setScene(sceneProgressIndicator);
              primaryStageProgressIndicator.setResizable(false);
              primaryStageProgressIndicator.setHeight(311);
              primaryStageProgressIndicator.setWidth(523);
              primaryStageProgressIndicator.show();
          } catch (IOException e) {
  
              e.printStackTrace();
          }
  
  
  
  
          // TODO Auto-generated method stub
  
  
  
      }
  
      public void onButton1Clicked() {
  
          FXMLLoader fxmlLoaderProgressIndicator = new FXMLLoader();
          fxmlLoaderProgressIndicator.setLocation(getClass().getResource("ProgressIndicator.fxml"));
          try {
              Parent root = fxmlLoaderProgressIndicator.load();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          progressIndicatorController = (ProgressIndicatorController) fxmlLoaderProgressIndicator.getController();
  
  
  
          for(double  i = 0.0; i &lt;= 1.0 ; i+=0.2) {
              progressIndicator = progressIndicatorController.getProgressIndicator1();
              progressIndicator.setProgress(i);
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
      }
  
  
  }
  </code></pre>
  
  <p><strong>ProgressIndicator.fxml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.ProgressIndicator?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.layout.Pane?&gt;
  
  
  &lt;AnchorPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ProgressIndicatorController"&gt;
     &lt;children&gt;
        &lt;Pane prefHeight="200.0" prefWidth="200.0"&gt;
           &lt;children&gt;
              &lt;ProgressIndicator fx:id="progressIndicator1" layoutX="47.0" layoutY="31.0" prefHeight="103.0" prefWidth="106.0" progress="0.0" /&gt;
           &lt;/children&gt;
        &lt;/Pane&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p><strong>ProgressindicatorController.java</strong></p>
  
  <pre><code>package application;
  
  import java.io.IOException;
  import java.net.URL;
  import java.util.ResourceBundle;
  
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.ProgressIndicator;
  import javafx.scene.image.Image;
  import javafx.scene.paint.Color;
  import javafx.stage.Stage;
  
  public class ProgressIndicatorController implements Initializable {
  
  
      @FXML
      private ProgressIndicator progressIndicator1;
  
      @Override
      public void initialize(URL location, ResourceBundle resources) {
  
  
  
  
          // TODO Auto-generated method stub
  
      }
  
      public ProgressIndicator getProgressIndicator1() {
  
          System.out.println("getteeerrrrrrrr");
          return progressIndicator1;
      }
  
      public void setProgressIndicator1(ProgressIndicator progressIndicator1) {
          this.progressIndicator1 = progressIndicator1;
      }
  
  }
  </code></pre>
  
  <p>The getter method is successfully called. 
  However when I click the button, the progress indicator doesnot get updated.</p>
  
  <p>I am also not getting any kind of error messages .</p>
  
  <p>I came across this link <a href="https://stackoverflow.com/questions/45864503/javafx-how-to-acces-fxml-objects">Javafx - how to acces FXML &quot;objects&quot;</a>   which indeed helped me to a great extent,but I'm not able to get this work too. I just tried to modify the button click method as follows :</p>
  
  <p><strong>onButton1Cliked()</strong></p>
  
  <pre><code>public void onButton1Clicked() {
  
          progressIndicator = (ProgressIndicator) sceneProgressIndicator.lookup("#progressIndicator1");
  
          for(double  i = 0.0; i &lt;= 1.0 ; i+=0.2) {
              try {
                  System.out.println("Progressingggg   " + i);
                  progressIndicator.setProgress(i);
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
  
      }
  </code></pre>
  
  <p>Clicking on the button does indeed print out the statements. But the progress indicator doesn't get updated with each for loop iteration. Its only at last that the progress indicator gets updated and shows done.</p>
  
  <p>Any idea why this is happening ?</p>
  
  <p>Also is there any other way to access the fxml elements other than using a lookup method. Can my previous approach(trying to get the fxml elements through controller)  be made to work somehow.</p>
  
  <p>Any advice or suggestions are most welcome. Been into this for several hours. Stackoverflow seems to be the last resort.</p>
  
  <p><strong>EDIT 1 : START</strong></p>
  
  <p>It turned out that the progress Indicator was not updating because of the reason explained in Zephyr's first comment. So i created a separte thread for my application and that seemed to work fine as the progress indicator got updated as expected.</p>
  
  <p><strong>Working version of onButton1Clicked() method that is successfully updating Progress Indicator  :</strong></p>
  
  <pre><code>public void onButton1Clicked() {
  
          progressIndicator = (ProgressIndicator) sceneProgressIndicator.lookup("#progressIndicator1");
  
          Runnable runnable = new Runnable() {
  
              @Override
              public void run() {
  
                  for(double  i = 0.0; i &lt;= 1.0 ; i+=0.2) {               
                      try {
                          System.out.println("Progressingggg   " + i);
                          progressIndicator.setProgress(i);
                          Thread.sleep(1000);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
          }   
  
              }
          };
  
          Thread thread = new Thread(runnable);
          thread.start();
  
      }
  </code></pre>
  
  <p>Having resolved this threading issue, I tried once again to go by the original way mentioned in my OP(i.e. to get the ProgressIndicatorController instance and access its progress indicator and then try updating it). However, this is not working for me. I tried debugging and found that I am getting back the ProgressIndicatorController and ProgressIndicator instance back.</p>
  
  <p>Why then my code is unable to update the progress indicator if it has a hold on the progress indicator instance.
  The println methods are executed successfully.Its just the progress indicator not getting updated at all.It stands at "0" all the time.</p>
  
  <p><strong>Version of onButton1Clicked() method that is NOT updating progressIndicator :</strong></p>
  
  <pre><code>public void onButton1Clicked() {        
  
          FXMLLoader fxmlLoaderProgressIndicator = new FXMLLoader();
          fxmlLoaderProgressIndicator.setLocation(getClass().getResource("ProgressIndicator.fxml"));
          try {
              Parent root = fxmlLoaderProgressIndicator.load();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          progressIndicatorController = (ProgressIndicatorController) fxmlLoaderProgressIndicator.getController();
  
  
  
          progressIndicator = progressIndicatorController.getProgressIndicator1();
  
          Runnable runnable = new Runnable() {
  
              @Override
              public void run() {
  
                  for(double  i = 0.0; i &lt;= 1.0 ; i+=0.2) {               
                      try {
                          System.out.println("Progressingggg   " + i);
                          progressIndicator.setProgress(i);
                          Thread.sleep(1000);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
          }   
  
              }
          };
  
          Thread thread = new Thread(runnable);
          thread.start();
  
      }
  </code></pre>
  
  <p><strong>EDIT 1 : END</strong></p>
  
  <p><strong>EDIT 2 : START</strong></p>
  
  <p><strong>Adding other files required to run this sample application</strong> </p>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>package application;
  
  import javafx.application.Application;
  import javafx.stage.Stage;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.layout.BorderPane;
  import javafx.fxml.FXMLLoader;
  
  
  public class Main extends Application {
      @Override
      public void start(Stage primaryStage) {
          try {
              Object root =FXMLLoader.load(getClass().getResource("Button.fxml"));
              Scene scene = new Scene((Parent) root,400,400);
              scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
              primaryStage.setScene(scene);
              primaryStage.show();
          } catch(Exception e) {
              e.printStackTrace();
          }
  
  
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>My <strong>application.css</strong> file has nothing in it. Its empty.</p>
  
  <p>I'm using <strong>JavaFX8</strong> on <strong>Java 8</strong>. IDE is <strong>Eclipse Photon</strong></p>
  
  <p>I created my project using <strong>e(fx)clipse</strong> plugin and choosing <strong>JavaFX Project</strong> from create new project window.</p>
  
  <p><strong>EDIT 2 : END</strong></p>
  
  <p><strong>EDIT 3 : START</strong></p>
  
  <p>Under the section <strong>EDIT 1</strong> while trying to test the version of <strong>onButton1Clicked()</strong> that successfully updates the progress indicator please don't forget to include <code>private Scene sceneProgressIndicator</code> as the member variable of the class <strong>ButtonController.java</strong>.</p>
  
  <p><strong>EDIT 3 : END</strong></p>