Applying styles to JavaFX nodes
Loading a CSS file is not the only option to work with styles. You can also set them directly through an API or specify them in the FXML file. And while this gives additional flexibility, it also requires you to be careful about the priorities of different approaches.
We'll review all the options and see how they interact in the next sections.
Having several CSS files in one JavaFX application
Note that you can use several style sheets simultaneously and apply them not only to Scene
but to an instance of the class javafx.scene.Parent
(which most JavaFX nodes are, except primitive shapes).
CSS assigned to a Parent
will be applied only to this object and all nodes in its children hierarchy.
Using the setStyle() method
The Node.setStyle(String style)
method can be called directly from the JavaFX code to apply CSS to the given node.
This method is not recommended for use in production applications due to worse performance than other approaches.
But it becomes irreplaceable...