Okay. So I have tried doing this for a few hours know, and I know there are a lot of topics on the subject, like for instance this thread. But I've tried everything suggested in that thread and a few others, with no success.
My project hierarchy (and package hierarchy) looks like this: myProject/src/com/company/app/view
And the class in which I'm trying to link to another file in the project is in the last directory, "view".
And the file I'm trying to link to is placed in "myProject/src/com/company/app/" and is named "stylesheet.css".
If I do System.out.println(new java.io.File("").getAbsolutePath());
I get the following path: "C:\Users\xxxx\workspace\myProject". Shouldn't this mean that I should specify an absolute path from myProject? In that case the path to my stylesheet would be "src/com/company/app/stylesheet.css", right? However, that doesn't work. I've also tried adding a slash in the beginning of that URL, which didn't work either.
I'm trying to add the stylesheet to a JavaFX DialogPane in my class. Like this:
dialogPane.getStylesheets().add("stylesheet.css");
and I've also tried this:
dialogPane.getStylesheets().add(getClass().getResource("stylesheet.css").toExternalForm());
The first version never works, but the second version works if I put stylesheet.css at the same location as my class. But when I try to find the stylesheet at its proper place, e.g:
dialogPane.getStylesheets().add(getClass().getResource("src/com/company/app/stylesheet.css").toExternalForm());
I get a NullPointerException
.
If I could just specify a path relative to the class location from the parent directory as you can in HTML, e.g "../stylesheet.css", that would be the answer. But it seems like you can't.
What am I doing wrong?