What is wrong with my syntax calling a stylesheet (css) from an FXML file?

I work in NetBeans 8 with Java 8 / JavaFX and FXML.

In the first controller (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 try to replace this with any type of relative path, at runtime I get the following error:

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 the subdirectory where it was)

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

I take care that I have a second problem. In the first version, which works on my machine ... when I take the jar file to another machine, css is not applied when the application is running ... It looks like a resource file that is not being copied.

reference

Thank!

-Adeena

+4
2

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

FXML,

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

layout.getStylesheets().add(getClass().
         getResource("/style/myStyle.css")).toExternalForm();
+5

:

<?import java.net.*?>

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

css (), FXML. <fx:root>, , <AnchorPane>.

docs, " " .

0

Source: https://habr.com/ru/post/1528974/


All Articles