JAVAFX - Load fxml with anchorpane inside main fxml


Question

I'm trying to load a fxml file (content area) inside a main fxml.

As the code is splited by several files i can't place here all code but if needed i will build a small version with the problem. I only place the relevant parts of code.

Main fxml file

...
    </MenuBar>
    <AnchorPane fx:id="mainContent" prefHeight="-1.0" prefWidth="-1.0"    VBox.vgrow="ALWAYS" />
    <ToolBar prefHeight="25.0" VBox.vgrow="NEVER" />
  </children>
</VBox>

Content fxml (take in attention the anchorpane id)

 ...
 <AnchorPane id="background" prefHeight="600.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
 <children>
   <Label minWidth="400.0" prefWidth="423.0" text="Tools" textAlignment="CENTER" textFill="WHITE" textOverrun="CLIP" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
  <effect>
    <Glow level="1.0" />
  </effect>
  <font>
    <Font name="Verdana Bold" size="45.0" />
  </font>
</Label>
<Label text="Version 0.01 alfa" textFill="WHITE" AnchorPane.rightAnchor="140.0" AnchorPane.topAnchor="69.0">
  <font>
    <Font name="Verdana" size="20.0" />
  </font>
</Label>
<Label text="2014 - José Longo" textFill="WHITE" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" />
 </children>
 <effect>
   <InnerShadow />
 </effect>
 </AnchorPane>

MainApp.java

...
    @Override
public void start(Stage stage) throws Exception {

    FXMLLoader loader = new FXMLLoader();
    AnchorPane mainScreen = (AnchorPane) loader.load(MainApp.class.getResource("/net/somewhere/fxml/Scene.fxml"));
    Scene scene = new Scene(mainScreen);
    scene.getStylesheets().add("/net/somewhere/styles/Styles.css");
    FXMLController mainController = loader.getController();

    ScreenNavigator.setMainControler(mainController);
    ScreenNavigator.setContent(ScreenNavigator.BACKGROUND); // if i remove/comment this the stage apears ok
    stage.setTitle("Tools");
    stage.setScene(scene);
    stage.show();
}

Controller class

public void setScreen(AnchorPane anchorPane){
    mainContent.getChildren().clear();
    mainContent.getChildren().addAll(anchorPane);
}

Content controller

public class ScreenNavigator {

//fxml files
public static final String BACKGROUND = "/net/somewhere/fxml/Background.fxml";

private static FXMLController mainController;

public static void setMainControler(FXMLController mainController){
    ScreenNavigator.mainController = mainController;
}

public static void setContent(String fxmlFile){

    try {
        URL url = ScreenNavigator.class.getResource(fxmlFile);
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(url);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        AnchorPane content = (AnchorPane) loader.load(url.openStream());
        System.out.println(content.getId()); //not null the object  i can get the id 
        mainController.setScreen(content); // i get java.lang.NullPointerException at net.somewhere.controllers.ScreenNavigator.setContent(ScreenNavigator.java:40)
    } catch (IOException ex) {
        Logger.getLogger(ScreenNavigator.class.getName()).log(Level.SEVERE, null, ex);
    }

}
}

And when try to run i get this error:

 --- exec-maven-plugin:1.2.1:exec (default-cli) @ Tools ---

background  <---- i can get the id of anchorpane
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
 Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:744)
 Caused by: java.lang.NullPointerException
at net.somewhere.controllers.ScreenNavigator.setContent(ScreenNavigator.java:40) <----
at net.somewhere.MainApp.start(MainApp.java:30)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more

I have tried with getResource(), getResourceAsStream() with Node, Pane, etc... and finally ended with a example present here at this site, but without success... always get this type of error...

Can someone point me a direction to solve this problem?

TIA

JL

1
0
3/6/2014 11:26:46 PM

After some "fights" with the code i finally found a solution...

For the ones as me who are looking for a way i provide a full example netbeans project you can get here

Screens of simple working example:

I can't post images yet... links for them

here

If you go to "Help-> About" you will get the help content as on screen below

link on comments...

If you hit button "Close" on this screen, you will get initial screen.

Regards

JL

0
3/7/2014 3:18:43 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon