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.
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.
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.
put your yourname.css
file directly under src directory.
scene.getStylesheets().add("yourname.css")
clean and build required
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With