Migrate from Javafx2.2 to Javafx8

2020-07-18 20:50发布

问题:

I am trying to migrate Javafx2.2 application to Javafx8. I am getting following issue while using nested FXML:

javafx.fxml.LoadException: Root hasn't been set.
Use method setRoot() before load.

FXML file:

<fx:root type="javafx.scene.layout.VBox" 
         xmlns:fx="http://javafx.com/fxml"
         xmlns="http://javafx.com/javafx/8"
         fx:controller="com.ui.TestController">
    <TextField fx:id="textField"/>
    <Button text="Click Me"/>
</fx:root>

Code:

FXMLLoader loader = new FXMLLoader();
loader.setResources(bundle);
InputStream in = Main.class.getClassLoader().getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(Main.class.getResource(fxml));
Node page = null;
try {
    page = (Node) loader.load(in);
} catch (IOException e) {
    logger.error("{}", e);
} finally {
    try {
        in.close();
    } catch (IOException e) {
        logger.error("{}", e);
    }
}

I need page node to set in another BorderPane.center. It works with Javafx2.2. What I am missing here? Any help would be appreciated.

回答1:

Using <VBox></VBox> instead of <fx:root type="javafx.scene.layout.VBox"></fx:root> has fixed this issue, atleast for Javafx build b121.



标签: fxml javafx-8