Indigo
Indigo

Reputation: 2997

CSS StyleSheet added in Scene Builder not found

I am getting an error in Netbeans While applying CSS style-sheet to JavaFX application through Scene Builder.

Error message in Netbeans is:

null/FXMLDocument.css Sep 17, 2014 12:44:43 AM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged

WARNING: Resource "FXMLDocument.css" not found.

All I did is to add the css file located in the same source folder as the FXMLDocument.fxml file is.

This is how I am applying CSS in Scene Builder to Main AnchorPane

Beginning of FXML looks like this:

<AnchorPane id="AnchorPane" fx:id="mainWindowPane" focusTraversable="true" stylesheets="@FXMLDocument.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="a.b.c.d.ui.FXMLDocumentController">

Any idea why is this happening, what exactly am I missing here and any suggestions to resolve this.

Update:

This is the start method:

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

    try {
        setUserAgentStylesheet(STYLESHEET_MODENA);
        FXMLLoader loader = new FXMLLoader();
        Parent root = (Parent) loader.load(getClass().getResourceAsStream("ui/ParentWindow.fxml"));
        final ParentWindowController controller = (ParentWindowController) loader.getController();

        stage.addEventHandler(WindowEvent.WINDOW_SHOWN, controller::handleWindowShownEvent);
        stage.addEventHandler(WindowEvent.WINDOW_SHOWING, controller::handleWindowShowingEvent);
        stage.addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, controller::handleWindowClosingRequestedEvent);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setResizable(false);
        stage.toFront();
        stage.setTitle("Sample Code");
        stage.getIcons().add(new Image(getClass().getResourceAsStream("resources/images/Logo.jpg")));
        stage.show();
    } catch (IOException iOException) {
        iOException.printStackTrace();
    }
}

Is this because of the FXMLLoader loader = new FXMLLoader(); object that JavaFX is not able to resolve the absolute path to css in FXML and then null/FXMLDocument.css

Adding below lines in start method works fine though.

scene.getStylesheets().setAll(
getClass().getResource("ui/FXMLDocument.css").toExternalForm()
);

Upvotes: 2

Views: 3299

Answers (1)

Tomas Bisciak
Tomas Bisciak

Reputation: 2841

Im looking into one of mayne implementation.And i use package specific to css and point to it via ..\css\backgroundc.css

Notice ..\ which points to this location

If you have css in the same package as a fxml just add ..\FXMLDocument.css That shoud fix your problem

If you decide it to initialize it via code you can use: for instance initialization of Scene with custom CSS Design

 scene.getStylesheets().setAll(
                    getClass().getResource(CSS_LOCATION).toExternalForm()
            );

As for CSS_LOCATION it might be for example:

/myapp/ui/css/style.css

Or just set some of the style with string for instance

node.setStyle("-fx-background-color:RED;");

^^btw This doesnt limit you to declare only one style/within string ; is delimiter and you can add more.As in normal css file.

Upvotes: 1

Related Questions