Error loading fxml file in a JavaFX project


Question

I have been working on a desktop application, where I need to use JavaFX. I have created an fxml file in a JavaFX project on eclipse. I build the scene in scene builder.when I am trying to run the main Java file, the changes which are made using scene builder on the fxml file are not getting reflected on the main window.and the screen is displayed blank. here is my code.

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        AnchorPane  root = new AnchorPane();

        Scene scene = new Scene(root,400,400,Color.BLACK);
        scene.getStylesheets().add(getClass().getResource("/application/sample.fxml").getPath());
        primaryStage.setTitle("FXML Welcome");

        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

And this is my sample.fxml file code:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?scenebuilder-background-color 0x00ffa3ff?>

<AnchorPane prefHeight="379.0" prefWidth="549.0001220703125" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">

<children>
<Button layoutX="219.0" layoutY="156.0" mnemonicParsing="false" text="hello" />
</children>
</AnchorPane>

The error I got was:

     WARNING: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged Resource "/F:/eclipse_progs/examplefx/bin/application/sample.fxml" not found.

But the location in the error specified contains the file it is referring to. Can anyone explain what might be the reason with the error. Is it with the code or is with the issues of plugin?

1
2
3/28/2014 12:03:25 PM

Accepted Answer

In JavaFX, if you want to load the a fxml file, then use the FXMLLoader as,

FXMLLoader.load(getClass().getResource("application/sample.fxml"));

To load stylesheet use as.

scene.getStlyeShees().add(getClass().getResource("application/sample.css").toExternalForm);

In your code, you are adding the fxml file in the stylesheets list of the scene which is wrong. Try using FXMLLoader as said above to load your fxml file.

Refer docs: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm

3
3/20/2014 1:00:17 PM

Try this start method and place your directory application/<NAME>.fxml in the same directory where your Main.class is fxml files getting loaded with the static FXMLLoader.load() method with getStylesheets.add(...) you can add .css files to your scene.

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

        Parent parent = FXMLLoader.load(getClass().getResource("application/demo.fxml"));
        Scene scene = new Scene(parent);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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