Problems with javaFX building path MVC fxml


Question

I am currently working on an administrator build in java FX 2.1 using netbeans 7.2

I have the following issues:

I am developing this particular tool in an MVC pattern, so I've created 3 packages called Model, view and Controller.

My problem is that when building the project in netbeans it would only read the files supposed to be in the view package if they're outside of it. Let me give you a context path:

.../administradorInfinix/view/
.../administradorInfinix/controller/
.../administradorInfinix/model

so it would only read the fxml files regarding the view if they are outside the view package (.../administradorInfinix/)

This is where I set the address of the file:

    private void irInicioSesion() {
    try {
        replaceSceneContent("InicioSesion.fxml");
    } catch (Exception ex) {
        Logger.getLogger(AdministradorINFINIX.class.getName()).log(Level.SEVERE, null, ex);
    }
}

You can see the file name is InicioSesion.fxml, which should be inside the view package but it won't load if this is the case.

This is the replaceSceneContent I'm using to search for the fxml files:

private Parent replaceSceneContent(String fxml) throws Exception {
    Parent page = (Parent) FXMLLoader.load(AdministradorINFINIX.class.getResource(fxml), null, new JavaFXBuilderFactory());
    Scene scene = stage.getScene();
    if (scene == null) {
        scene = new Scene(page,548,416);
        //scene.getStylesheets().add(AdministradorINFINIX.class.getResource("demo.css").toExternalForm());
        stage.setScene(scene);
    } else {
        stage.getScene().setRoot(page);
    }
    stage.sizeToScene();
    return page;
}

And this is the error it gives me when trying to run (it builds just fine but it won't run)

> administradorinfinix.AdministradorINFINIX irInicioSesion
Grave: null
java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at administradorinfinix.AdministradorINFINIX.replaceSceneContent(AdministradorINFINIX.java:126)
    at administradorinfinix.AdministradorINFINIX.irInicioSesion(AdministradorINFINIX.java:110)
    at administradorinfinix.AdministradorINFINIX.start(AdministradorINFINIX.java:46)
    at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:722)

where line 110 is

  replaceSceneContent("InicioSesion.fxml");

and line 126 is

Parent page = (Parent) FXMLLoader.load(AdministradorINFINIX.class.getResource(fxml), null, new JavaFXBuilderFactory());

I hope you can help me fix this problem.

1
1
9/21/2012 7:36:52 PM

Accepted Answer

You need to call the method FXMLLoader#setLocation with the URL of the FXML file. Have a look at the following source for an example of how to load FXML files:

https://github.com/cathive/fx-guice/blob/master/src/main/java/com/cathive/fx/guice/GuiceFXMLLoader.java

2
3/12/2015 8:56:29 PM

The FXMLLoader fails to locate the .fxml-file. The problem is that your call to Class.getResource() returns null. The exception thrown by FXMLLoader.load(null) is quite misleading, it should rather be something such as an ArgumentNullException. You can fix the problem with loading your resource file by specifying the full package path, in my case a call like this works:

    FXMLLoader loader = new FXMLLoader(new Employee().getClass().getResource("/de/mycompany/mypackage/view/loginform.fxml"));

I hope this helps.


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