Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding css file to stylesheets in javafx

Language: JavaFX

IDE: Netbeans

Problem: I'm trying to add a css file to the stylesheet, but the first line of the following code always generates a NullPointerException:

String css = this.getClass().getResource("double_slider.css").toExternalForm(); 
scene.getStylesheets().add(css);

I've tried replacing "double_slider.css" with the full path. double_slider.css is currently located in the same package as the class that makes this call. I've also tried all of the variations found at http://introjava.wordpress.com/2012/03/21/linking-a-css-style-sheet-to-javafx-scene-graph/, with no success. Clean and build doesn't help either.

If I place the css file in the build folder where the .class files are dumped, the NullPointerException goes away. But then the css file does not work properly because it references other files in my project.

like image 718
Danielle Avatar asked Dec 19 '12 05:12

Danielle


People also ask

Where do I put CSS in JavaFX?

The default css for all JavaFX applications is written in a file called modena. css , which can be found in the JavaFX runtime jar file, jfxt. jar , located in your Java installation folder. This css file defines the styles for the root node and the UI controls.

How do I add CSS to Scene Builder?

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

put your yourname.css file directly under src directory.

scene.getStylesheets().add("yourname.css")

clean and build required

like image 63
nikolaos Avatar answered Oct 06 '22 17:10

nikolaos


I think your're missing the slashes which causes that the CSS file can't be found. Try to correct your path reference.

For example:

-root
--app/Main.java
--assets/double_slider.css

would be:

String css = this.getClass().getResource("/assets/double_slider.css").toExternalForm();
like image 34
Korki Korkig Avatar answered Oct 06 '22 17:10

Korki Korkig