cmtjk
cmtjk

Reputation: 359

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!

Upvotes: 1

Views: 849

Answers (3)

clartaq
clartaq

Reputation: 5372

Not sure of your development setup, but I usually grab resources from the JAR file created by my IDE. If you are using fxml, you can start with the URL passed as an argument to the start() method. Then I use something like the following:

        private String getCssFileLocation() {
            String locInJar = "/net/clarkonium/jist/resources/css/help.css";
            System.out.println("fxml URL: " + fxmlUrl);
            String externalForm = fxmlUrl.toExternalForm();
            System.out.println("fxmUrl.toExternalForm(): " + externalForm);
            String prefix = externalForm.substring(0, externalForm.indexOf("!") + 1);
            System.out.println("prefix: " + prefix);
            String resLocation = prefix + locInJar;
            System.out.println("resLocation: " + resLocation);
            return resLocation;
        }

Since my program knows the location of the resource it wants, it pastes that on the URL in place of the fxml location. A run of the program produced this output showing the steps the function takes:

fxml URL: jar:file:/C:/projects/Jist/dist/run786853897/Jist.jar!/net/clarkonium/jist/RepositorySetup.fxml
fxmUrl.toExternalForm(): jar:file:/C:/projects/Jist/dist/run786853897/Jist.jar!/net/clarkonium/jist/RepositorySetup.fxml
prefix: jar:file:/C:/projects/Jist/dist/run786853897/Jist.jar!
resLocation: jar:file:/C:/projects/Jist/dist/run786853897/Jist.jar!/net/clarkonium/jist/resources/css/help.css

Then you can use the location returned to add the stylesheet.

All the stuff before the exclamation point is dependent on my IDE and changes from run to run. This method also works after development is finished and the program is run from another location.

Upvotes: 0

brian
brian

Reputation: 10989

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

Upvotes: 2

RealSkeptic
RealSkeptic

Reputation: 34638

On Windows, the appropriate way to create a "file" URI is to add three slashes before the drive letter: C:\something\something becomes file:///C:/something/something.

Better yet, since you are using a Path, simply use Path.toUri(). It will do this properly for you both on Linux and on Windows.

Upvotes: 1

Related Questions