Search code examples
javacssjavafxstylesheet

getStylesheets().add(...) not working with Windows (7)


(Scroll down for solution)

I've made a program with JavaFX with Linux. I use

scene.getStylesheets().add(
            "file:" + Paths.get(System.getProperty("user.dir") + "/resources/style/stylesheet.css").toString());

to load my stylesheet into the application. This works under Linux but not under Windows. Whatever I do it always says

Aug 01, 2015 5:53:42 PM com.sun.javafx.css.StyleManager loadStyleSheetUnPrivileged
Warning: Resource "file:C:\Users\win7\Desktop\resources\style\stylesheet.css" not found.

Anyone an idea what's the problem her? Any keywords?

Thank you in advance!

UPDATE: I've tried

scene.getStylesheets().add(
            "file:///" + Paths.get(System.getProperty("user.dir") + "/resources/style/stylesheet.css").toString());

and

    URI stylesheetURI = Paths.get(System.getProperty("user.dir") + "/resources/style/stylessheet.css").toUri();
    scene.getStylesheets().add(stylesheetURI.toString());

and

    Path stylesheetPath = Paths.get(System.getProperty("user.dir") + "/resources/style/stylessheet.css");
    scene.getStylesheets().add(stylesheetPath.toUri().toString());

SOLUTION: I've got it working for Linux and Windows with the following code:

scene.getStylesheets().add(Paths.get("./resources/style/stylesheet.css").toAbsolutePath().toUri().toString());

with the help of @RealSkeptic and @brian. Thanks!


Solution

  • The user.dir is just the current dir where the program was started. Put a . to say it's a path relative to the current dir. Then convert it to a proper string as shown.

    File f = new File("./resources/style/stylesheet.css");
    scene.getStylesheets().add(f.toURI().toURL().toExternalForm());
    

    add: I looked up the definition of user.dir and the java people don't use the terminology current dir. Instead, they say

    "user.dir" User working directory