title: How to use FXMLLoader.load() - JavaFX 2
body: <p>I am building a <a href="http://docs.oracle.com/javafx/index.html" rel="noreferrer">JavaFX</a> application using the <a href="http://www.oracle.com/technetwork/java/javafx/tools/index.html" rel="noreferrer">JavaFX Scene Builder</a>. The interface was created in the Scene Builder and a <a href="http://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html" rel="noreferrer">FXML</a> file (main.fxml) was created.</p>
  
  <p>To use the interface in my application I must load the FXML file using the <a href="http://docs.oracle.com/javafx/2/api/javafx/fxml/FXMLLoader.html" rel="noreferrer">FXMLLoader</a>, but there is a problem because the load() method returns an Object, and to build a Scene I need an instance of Parent class. </p>
  
  <p>Below is a piece of my MainClass. The compiler is giving an error because page is not of type Parent:</p>
  
  <pre><code>Object page = FXMLLoader.load(MainWindowController.class.getResource("main.fxml"));
  Scene scene = new Scene(page);
  primaryStage.setScene(scene);
  primaryStage.show();
  </code></pre>
  
  <p>What do I do to make this compile and run correctly?</p>
  
  <p>Here is the FXML file (main.fxml):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.paint.*?&gt;
  
  &lt;AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml"&gt;
    &lt;children&gt;
      &lt;VBox prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
        &lt;children&gt;
          &lt;MenuBar minHeight="21.0" prefHeight="21.0" prefWidth="600.0"&gt;
            &lt;menus&gt;
              &lt;Menu mnemonicParsing="false" text="File"&gt;
                &lt;items&gt;
                  &lt;MenuItem mnemonicParsing="false" text="Close" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
              &lt;Menu mnemonicParsing="false" text="Edit"&gt;
                &lt;items&gt;
                  &lt;MenuItem mnemonicParsing="false" text="Delete" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
              &lt;Menu mnemonicParsing="false" text="Help"&gt;
                &lt;items&gt;
                  &lt;MenuItem mnemonicParsing="false" text="About" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
            &lt;/menus&gt;
          &lt;/MenuBar&gt;
          &lt;ToolBar minHeight="24.0"&gt;
            &lt;items&gt;
              &lt;Button fx:id="btFormat" mnemonicParsing="false" text="FORMAT" /&gt;
              &lt;Button fx:id="btUnformat" mnemonicParsing="false" text="Unformat" /&gt;
              &lt;Button fx:id="btAutoIndent" mnemonicParsing="false" text="Auto-indent" /&gt;
              &lt;CheckBox fx:id="cbShowLineNumber" mnemonicParsing="false" text="Show line number" /&gt;
            &lt;/items&gt;
          &lt;/ToolBar&gt;
          &lt;SplitPane dividerPositions="0.5" focusTraversable="true" orientation="VERTICAL" prefHeight="348.0" prefWidth="600.0"&gt;
            &lt;items&gt;
              &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"&gt;
                &lt;children&gt;
                  &lt;VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
                    &lt;children&gt;
                      &lt;Label text="INPUT - Paste your code here" /&gt;
                      &lt;TextArea fx:id="taInput" prefWidth="200.0" wrapText="true" /&gt;
                    &lt;/children&gt;
                  &lt;/VBox&gt;
                &lt;/children&gt;
              &lt;/AnchorPane&gt;
              &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"&gt;
                &lt;children&gt;
                  &lt;VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
                    &lt;children&gt;
                      &lt;Label text="OUTPUT - The formatted code" /&gt;
                      &lt;TextArea fx:id="taOutput" prefWidth="200.0" wrapText="true" /&gt;
                    &lt;/children&gt;
                  &lt;/VBox&gt;
                &lt;/children&gt;
              &lt;/AnchorPane&gt;
            &lt;/items&gt;
          &lt;/SplitPane&gt;
        &lt;/children&gt;
      &lt;/VBox&gt;
    &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>