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 ?

1

4 Answers 4

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());
Sign up to request clarification or add additional context in comments.

Comments

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") 

1 Comment

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())
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.

Comments

2

Simply add package name with .css file.

Your code will look like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.