Mazzone
Mazzone

Reputation: 308

"loadStylesheetUnPrivileged" error when trying to use css stylesheet with JavaFX

I've read through every article/post I can find about this error, and I've tried every solution mentioned and the error is still being produced at run time. So here's my code, and below that is the error message from the console:

public class Driver extends Application {

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

@Override
public void start(Stage primaryStage) {

    Parent root = null;
    File css = new File("stylesheet.css");

    try {
        root = FXMLLoader.load(getClass().getResource("project-3.fxml"));
        root.getStylesheets().clear();
        root.getStylesheets().add("file:///" + css.getAbsolutePath().replace("\\", "/"));
    } catch (IOException e) {
        System.out.println(e);
        System.exit(1);
    } // try

    primaryStage.setTitle("Programmer's Calculator");
    primaryStage.setScene(new Scene(root, 397, 376));
    primaryStage.show();



} // start

} // Driver

I've excluded the import statements to save space - they're not the issue.

Here's the error produced:

com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO: Could not find stylesheet: file:////Users/UserName/Documents/Names-p3/stylesheet.css

Here's my directory:

Directory Structure

Here's what I've tried:

Literally nothing is working. What is going on?

Upvotes: 4

Views: 27165

Answers (3)

Atspulgs
Atspulgs

Reputation: 1439

The following have worked for me:

  • Resources (file has to be with the class files): scene.getStylesheets().add(getClass().getResource("style.css").toString());
  • Relative (files in working dir): scene.getStylesheets().add("file:style.css");
  • Absolute (files still in working dir): scene.getStylesheets().add("file:/"+System.getProperty("user.dir").replace("\\", "/")+"/style.css");

It would seem that it's looking for the format of: file:<path> and for absolute paths its still looking for that root '/' even on windows. It also doesn't seem to like backslash '\' in the path.

Upvotes: 2

Vaclav Konecny
Vaclav Konecny

Reputation: 319

One simple solution is add at sign to FXML like this.

<Pane stylesheets="@stylesheet.css">

rather than:

<Pane stylesheets="stylesheet.css">

More info https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html

Upvotes: 4

Collins Abitekaniza
Collins Abitekaniza

Reputation: 4588

Put the file containing your stylesheet in the src folder and then apply it to your root.

root = FXMLLoader.load(getClass().getResource("project-3.fxml")); 
root.getStylesheets().add(getClass().getResource("your_stylesheet.css").toExternalFo‌​rm());

Or

root.getStylesheets().add(getClass().getResource("your_stylesheet.css").toString());

Upvotes: 2

Related Questions