6

This code is not working in javafx 8

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

I get the 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 the css ?

Neuron
  • 5,141
  • 5
  • 38
  • 59
java baba
  • 2,199
  • 13
  • 33
  • 45
  • 1
    Maybe this helps [CSS loading problem with javafx][1] [1]: http://stackoverflow.com/questions/9010989/javafx-2-how-to-load-stylesheet-into-a-scene-subclass-distributed-as-part-of-a – Inge Mar 25 '14 at 08:41

4 Answers4

12

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());
int lawl is over 9000
  • 977
  • 1
  • 15
  • 25
3

In my case ,I want to load a css file from disk and I do it as this article said and it worked for me. code snippet as follow:

scene.getStylesheets().add("file:///E:/csse2002-7023/src/csse2002/block/world/main.css") 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
aolphn
  • 2,950
  • 2
  • 21
  • 30
  • 1
    This was the answer I was looking for. Using Paths you get the the same result with: Paths.get("E","csse2002-7023","src","csse2002","block","world","main.css").toUri().toString()) – Rudy Barbieri Jun 09 '20 at 09:19
2

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.

nllsdfx
  • 900
  • 14
  • 30
2

Simply add package name with .css file.

Your code will look like this:

scene.getStylesheets().add("sample/appCssFile.css");
Usman Khan
  • 3,739
  • 6
  • 41
  • 89
firoz Miya
  • 21
  • 2