How load css file in javafx8


Question

javafx 8 this code is not working

  scene.getStylesheets().add("appCssFile.css");

give Exception

Mar 25, 2014 12:21:20 PM com.sun.javafx.css.parser.CSSParser reportException
WARNING: Please report java.lang.NumberFormatException at:
Mar 25, 2014 12:21:20 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "appCssFile.css" not found. 

How can i load css ?

1
2
3/25/2014 7:05:20 AM

Accepted Answer

You need an URL and call toExternalForm in order to load a css file into your project.
Use the ClassLoader for that:

scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
7
3/31/2014 7:00:53 AM

For those who can't find css the way from accepted answer use classLoader:

getClass().getClassLoader().getResource("css/style.css").toExternalForm()

If you use maven it will find your css under resources/css/ dir.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon