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 Nu
in your file .java use this
Application.setUserAgentStylesheet(getClass().getResource("application.css")
.toExternalForm());
Have you initialized the scene object prior to setting style sheet?
scene = new Scene(myRootLayout, 600, 600); // for example
Assuming the file structure is something like this:
-root
--src
---resources
----double_slider.css
---package
----JavaFXFile.java
This is what worked for me:
scene.getStylesheets().add((new File("src/resources/double_slider.css")).toURI().toString());
In a maven project you must define path for resources in the file.pom in the javafx-plugin section: something like this:
<configuration>
<mainClass>com.personale.ciaomondo.App</mainClass>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
Folder Structure
In the MedicalLibraryFx.java
scene.getStylesheets().add(getClass().getResource("/css/style.css").toString());
Folder structure when css is in the same directory as controller
scene.getStylesheets().add(getClass().getResource("style.css").toString());
put your yourname.css
file directly under src directory.
scene.getStylesheets().add("yourname.css")
clean and build required