5

I am working in NetBeans 8, with Java 8 / JavaFX and FXML.

In the first controller (an AnchorPane) in my FXML document, I have this, that works:

stylesheets="file:///C:/Users/me/Documents/NetBeansProjects/MyApp/src/myApp/myStyle.css"

However, when I attempt to replace that with any type of relative path, I get the following error at runtime:

com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "myStyle.css" not found.

I tried

stylesheets="myStyle.css"
stylesheets="file://myStyle.css"
stylesheets="./myStyle.css"
stylesheets="css/myStyle.css"

(where I put a copy of the css file in a subdirectory from where it was)

...and maybe 50 other permutations. Every time I get the same error.

The reason I care is that I am having a second problem. In the first version, that works on my machine... when I take my jar file to another machine, the css is not applied when the application is run... It's like the resource file isn't copied.

Help?

Thanks!

-Adeena

adeena
  • 4,027
  • 15
  • 40
  • 52

2 Answers2

6

Let me consider your project structure as follows

MyApp
  |
  |_ src
      |
      |_controllers (Controllers)
      |_view (FXML)
      |_style (css)
          |_myStyle.css

You can specific the relative path in the FXML using

<stylesheets>
   <URL value="@../style/myStyle.css" />
</stylesheets>

In your controller, you can add it as follows

layout.getStylesheets().add(getClass().
         getResource("/style/myStyle.css")).toExternalForm();
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • Thanks! This worked for the stylesheets. Any other advice about referencing files in general? I'm having a different, but similar issue with referencing files. My file is in the same location as the src java, but "filename.jpg" (it's not a jpg but something proprietary) doesn't work, but referencing the absolute path does. – adeena Feb 26 '14 at 20:00
  • Can you show me a piece of code. For loading images in styles folder I would use something similar `Image image = new Image(getClass().getResourceAsStream("/style/edit.png"));` – ItachiUchiha Feb 26 '14 at 20:03
  • Unfortunately, I can't send this piece of code. The concept of getClass().getResourceAsStream("style/edit.png") doesn't work because it's only expecting a String. – adeena Feb 26 '14 at 20:06
  • @adeena The code I gave you is for loading images and not files. If its just files and not images that you are loading then you will first have to make a `File` and then read it. I am not sure what type of file it is, so I am actually in a fix how to help you out ! – ItachiUchiha Feb 26 '14 at 20:11
  • That's okay - thank you so much for trying! :) The original answer did answer my original question. :) – adeena Feb 26 '14 at 20:14
  • @adeena may be you could just tell me the `file type` that you are trying to read ? – ItachiUchiha Feb 26 '14 at 20:15
  • It's a ".g" file. It's a proprietary graphic. :) – adeena Feb 26 '14 at 20:16
  • Never heard of the file type :P . But, IMHO, there must be some Java API to read such files ! – ItachiUchiha Feb 26 '14 at 20:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48520/discussion-between-adeena-and-itachiuchiha) – adeena Feb 26 '14 at 20:23
1

This is how it works for me:

<?import java.net.*?>

<fx:root type="javafx.scene.layout.BorderPane" ... xmlns:fx="http://javafx.com/fxml">
  <stylesheets>
    <URL value="@myStyle.css" />
  </stylesheets>
</fx:root>

The css file is in the same package (folder) as the FXML. Also my root happens to be <fx:root>, I expect it to work the same for your <AnchorPane>.

Check out the docs, search for "Location Resolution" for details.

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90