Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load css file in javafx8

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 ?

like image 385
java baba Avatar asked Mar 25 '14 07:03

java baba


People also ask

How do I link CSS to JavaFX?

Adding it via the SceneBuilder or Hardcoding itIn the SceneBuilder view, click on the AnchorPane that was added first. In the Properties tab, under the 'JavaFX CSS' section, click on the 'Stylesheets' option. Select the CSS file, and that's it.

Can we use CSS in JavaFX?

You can use CSS in JavaFX applications similar to how you use CSS in HTML. You should have a basic understanding of CSS to get the most out of this tutorial.

How do I add a stylesheet to SceneBuilder?

In Scene Builder, you can simulate the attachment of a style sheet to an application Scene by selecting Preview, then Scene Style Sheets, and finally choosing Add a Style Sheet or Open a Style Sheet option. This Preview command is useful when the “root” style class is defined in the style sheet.


2 Answers

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());
like image 85
int lawl is over 9000 Avatar answered Oct 05 '22 03:10

int lawl is over 9000


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") 
like image 25
aolphn Avatar answered Oct 05 '22 02:10

aolphn