I'm using Java 1.8u25 and I am using a stylesheet as below (both Java and Clojure equivalents).
//Java
new Pane().getStylesheets().add("some_style.css")
//Clojure
(doto (Pane.)
(-> .getStylesheets (.add "some_style.css")))
I'm using Clojure, and I don't restart the JVM between test runs. I do, however, make a new GUI, of new instances, for each run. JavaFX caches the stylesheet somewhere, so editing the CSS file has no effect. Is there a way to force JavaFX to reload the stylesheet? Or, a way to reload classes so that the cache is lost?
Notice that you are not setting or changing the stylesheet but adding a stylesheet:
new Pane().getStylesheets().add("some_style.css")
You call the add()
method! So basically it's your Pane
that caches the previously loaded stylesheet.
Solution is simple: before loading it again, remove (clear) the old one:
Pane p = new Pane();
// Load first time:
p.getStylesheets().add("some_style.css");
// Later to reload:
p.getStylesheets().clear();
p.getStylesheets().add("some_style.css");
// some_style.css will be read again and applied
Note:
If you use multiple CSS files, clear()
might not be what you want here. In this case instead of clear()
you can remove a specific style sheet (which you want to reload) with the remove()
method before adding it again.
Note #2:
When loading CSS files, you should use URLs to resources, not just a file name. If only file name is provided, it must contain the package name too. Do it like this which works both from file system and from a jar file:
p.getStylesheets().add(YourClass.class.getResource("some_style.css")
.toExternalForm()); // if some_style.css is next to YourClass.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With