title: JavaFx Controller initialization is not working in proper order
body: <p>I am working on a Desktop application using JavaFX 2.2. I have an application class which loads the main fxml. In that main fxml i had a Border pane and in the center part, i had included a child fxml, using <code>&lt;fx:include&gt;</code>. My problem is that, the child fxml's controller initialize method is getting called before the parent controller's. I need to get the parent to be initialized when its loaded in the application class. I dont know whether its the actual or behavior or i need to do something else to get it works done. I am really new to the JavaFX.</p>
  
  <p>Below is my sample code.</p>
  
  <p>//Main Application class</p>
  
  <pre><code>public class DemoApplication extends Application {
  
      static double stage_width, stage_height;
  
      @Override
      public void start(Stage stage) throws Exception {
          Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
          stage_width = primScreenBounds.getWidth();
          stage_height = primScreenBounds.getHeight();
          Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
          stage.setX(0);
          stage.setY(0);
          stage.setWidth(primScreenBounds.getWidth());
          stage.setHeight(primScreenBounds.getHeight());
          Scene scene = new Scene(root);
          stage.setScene(scene);
          stage.show();
  
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  </code></pre>
  
  <p>//Main Controller</p>
  
  <pre><code>public class MainController implements Initializable {
   @Override
      public void initialize(URL url, ResourceBundle rb) {
         System.out.println("Initializing the parent controller");
      }
  }
  </code></pre>
  
  <p>//Child Controller</p>
  
  <pre><code>public class ChildController implements Initializable {
   @Override
      public void initialize(URL url, ResourceBundle rb) {
         System.out.println("Initializing the child controller");
      }
  }
  </code></pre>
  
  <p>//Main Fxml</p>
  
  <pre><code>&lt;AnchorPane id="AnchorPane" fx:id="ap_screen" prefHeight="821.0000999999975" prefWidth="1395.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.demo.app.controller.MainController"&gt;
    &lt;children&gt;
      &lt;BorderPane fx:id="bp" prefHeight="754.0" prefWidth="1282.0"&gt;
        &lt;center&gt;
          &lt;Pane fx:id="p2_cen" prefHeight="200.0" prefWidth="200.0"&gt;
            &lt;children&gt;
              &lt;HBox fx:id="p1_cen" prefHeight="100.0" prefWidth="523.0" styleClass="hb"&gt;
                &lt;children&gt;
                  &lt;Button mnemonicParsing="false" prefHeight="20.0" prefWidth="100.0" text="Favourite" textFill="WHITE" HBox.margin="$x2" /&gt;
                &lt;/children&gt;
                &lt;stylesheets&gt;
                  &lt;URL value="@styles.css" /&gt;
                &lt;/stylesheets&gt;
              &lt;/HBox&gt;
              **&lt;fx:include source="child.fxml" layoutY="100.0" /&gt;**
            &lt;/children&gt;
          &lt;/Pane&gt;
        &lt;/center&gt;
        &lt;left&gt;
          &lt;VBox fx:id="vb_left" prefHeight="419.0" prefWidth="200.0" styleClass="vbox"&gt;
            &lt;children&gt;
              &lt;ComboBox fx:id="combo" onAction="#combo" prefHeight="25.0" prefWidth="200.0" styleClass="combo"&gt;
                &lt;stylesheets&gt;
                  &lt;URL value="@styles.css" /&gt;
                &lt;/stylesheets&gt;
              &lt;/ComboBox&gt;
            &lt;/children&gt;
            &lt;stylesheets&gt;
              &lt;URL value="@styles.css" /&gt;
            &lt;/stylesheets&gt;
          &lt;/VBox&gt;
        &lt;/left&gt;
        &lt;top&gt;
          &lt;HBox fx:id="hb_top" prefHeight="100.0" prefWidth="500.0" styleClass="hbox"&gt;
          &lt;/HBox&gt;
        &lt;/top&gt;
      &lt;/BorderPane&gt;
    &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>//child fxml</p>
  
  <pre><code>&lt;AnchorPane id="AnchorPane" prefHeight="457.0" prefWidth="619.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.demo.app.controller.ChildController"&gt;
      &lt;children&gt;
          &lt;ScrollPane fx:id="sp1" fitToWidth="true" layoutY="189.0" prefHeight="200.0" prefWidth="364.0"&gt;
              &lt;content&gt;
                  &lt;AnchorPane id="Content" prefHeight="241.0" prefWidth="435.0"&gt;
                      &lt;children&gt;
                          &lt;VBox fx:id="vbmain" prefHeight="100.0" prefWidth="174.0" /&gt;
                          &lt;Button id="b1" fx:id="b2" layoutY="40.0" mnemonicParsing="false" onAction="#movleft" prefHeight="80.0" prefWidth="40.0" text="Button" /&gt;
                          &lt;Button id="b2" fx:id="b1" layoutX="246.0" layoutY="40.0" mnemonicParsing="false" onAction="#movright" prefHeight="80.0" prefWidth="40.0" text="Button" /&gt;
                      &lt;/children&gt;
                  &lt;/AnchorPane&gt;
              &lt;/content&gt;
          &lt;/ScrollPane&gt;
      &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>