How to use FXMLLoader.load() - JavaFX 2
Asked Answered
I

3

11

I am building a JavaFX application using the JavaFX Scene Builder. The interface was created in the Scene Builder and a FXML file (main.fxml) was created.

To use the interface in my application I must load the FXML file using the FXMLLoader, but there is a problem because the load() method returns an Object, and to build a Scene I need an instance of Parent class.

Below is a piece of my MainClass. The compiler is giving an error because page is not of type Parent:

Object page = FXMLLoader.load(MainWindowController.class.getResource("main.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.show();

What do I do to make this compile and run correctly?

Here is the FXML file (main.fxml):

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <VBox prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <children>
        <MenuBar minHeight="21.0" prefHeight="21.0" prefWidth="600.0">
          <menus>
            <Menu mnemonicParsing="false" text="File">
              <items>
                <MenuItem mnemonicParsing="false" text="Close" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Edit">
              <items>
                <MenuItem mnemonicParsing="false" text="Delete" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Help">
              <items>
                <MenuItem mnemonicParsing="false" text="About" />
              </items>
            </Menu>
          </menus>
        </MenuBar>
        <ToolBar minHeight="24.0">
          <items>
            <Button fx:id="btFormat" mnemonicParsing="false" text="FORMAT" />
            <Button fx:id="btUnformat" mnemonicParsing="false" text="Unformat" />
            <Button fx:id="btAutoIndent" mnemonicParsing="false" text="Auto-indent" />
            <CheckBox fx:id="cbShowLineNumber" mnemonicParsing="false" text="Show line number" />
          </items>
        </ToolBar>
        <SplitPane dividerPositions="0.5" focusTraversable="true" orientation="VERTICAL" prefHeight="348.0" prefWidth="600.0">
          <items>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
              <children>
                <VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                  <children>
                    <Label text="INPUT - Paste your code here" />
                    <TextArea fx:id="taInput" prefWidth="200.0" wrapText="true" />
                  </children>
                </VBox>
              </children>
            </AnchorPane>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
              <children>
                <VBox prefHeight="170.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                  <children>
                    <Label text="OUTPUT - The formatted code" />
                    <TextArea fx:id="taOutput" prefWidth="200.0" wrapText="true" />
                  </children>
                </VBox>
              </children>
            </AnchorPane>
          </items>
        </SplitPane>
      </children>
    </VBox>
  </children>
</AnchorPane>
Interne answered 6/12, 2012 at 23:3 Comment(0)
P
16

Due to a variety of entities eligible to be loaded through FXML you need to state the expected type yourself.

Parent page = FXMLLoader.<Parent>load(MainWindowController.class.getResource("main.fxml").toExternalForm());
Paddie answered 7/12, 2012 at 8:44 Comment(2)
Thanks for your answer, it is simpler than I thought. Based on your answer I did it like this and it also works: Parent page = (Parent)FXMLLoader.load( MainWindowController.class.getResource("main.fxml"));Interne
For Java >= 7 the cast is not necessary. Just use: FXMLLoader.load(...). Type inference will do the rest.Homochromous
I
9

Try this

Parent root= FXMLLoader.load(getClass().getResource("mention the path to your fxml file");

hope that this would help

Insufferable answered 24/6, 2014 at 8:59 Comment(0)
G
0
public class App extends Application {

    @Override
    public void start(Stage stage) throws IOException {

        Parent parent = FXMLLoader.load(getClass().getResource("/ui.fxml"));
        stage.setScene(new Scene(parent));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

If you are using a maven structure keep the ui.fxml in the resources directory.

When writing controllers in Java 9 and above

Genagenappe answered 24/4, 2022 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.