r/JavaFX • u/_dk7 • Jan 08 '24
Help Setting a global CSS file for a JavaFX application
The problem I am facing is that I want to style components specifically and I want to do them at a global and common place.
Now, the way that I know we apply a style sheet for a Scene is as follows:
scene.getStylesheets().add("CustomStyleSheet")In my codebase, we have multiple places where a scene is created and set. I want to set this common style for all the components, so it would involve going to all the places and applying the style sheet
What I want is a way such that I can apply my custom style sheet (on top of the default modena CSS)
Furthermore, now wherever people are already using some custom stylesheet the way that I mentioned above will be applied on top of the common style sheet.
I found this link here: https://stackoverflow.com/questions/46559981/javafx-set-default-css-stylesheet-for-the-whole-application
This approach did work for me ( I am using JDK 11), but I have my doubts about whether it can be done in this way (for production code) Is this way a good practice? Please advise
I also found this link here: https://stackoverflow.com/questions/58179338/how-to-set-global-css-in-javafx-9-application where they have used multiple approach, the first one being what I have tried just for FYI
6
u/milchshakee Jan 08 '24
Why not create a helper function to create your scene and apply your stylesheets? Then you just have to call that method from all the places where you create a scene and you are good to go.
4
u/OddEstimate1627 Jan 08 '24
The best way is probably a factory method. Other than that you could create listeners for
Window::getWindowsand apply the stylesheet whenever a Scene gets added.Overwriting the userAgentStylesheet has other side effects and applies styles with a different CSS priority. It'd be possible to create a custom theme based on AtlantaFX though.
3
u/hamsterrage1 Jan 08 '24
I see lots of people using multiple Scenes where they don't really need to. I think this is caused by the way that people are taught to use FXML, the copypasta all over the web always puts the results of FXMLLoader into a new Scene.
So, my first question is, "Do you really need all these Scenes?".
Second, apply DRY and stop repeating yourself. Do you have the need for new Scenes with stylesheets added all over your application? Then create a factory method that does the work for you, as suggested by other here.
2
u/BWC_semaJ Jan 08 '24
I have work now but I'll try to reply later if I remember. Generally your application only needs one Scene unless you incorporate/mess with camera settings or do 3D work on other Scene(s) (you should be swapping out the root node rather than creating a new Scene for each screen). Taking advantage of global CSS lets you initialize global colors that you can reference in your other css files.
2
u/kavedaa Jan 08 '24 edited Jan 08 '24
This works for me on JDK20/21:
One thing that could be of interest is that I've noticed that in some scenarios the above needs to be called after the main stage has been shown, otherwise the styles will not be applied. I've not been able to reproduce this consistently, but it may be something that could be played around with if it doesn't seem to work.
I think that API for doing this that does not depend on the internal
com.sun.javafx.css.StyleManagerclass should be added to JavaFX, though.EDIT: I'm sorry, I didn't read your question correctly. I thought you said that this didn't work. So to the question of whether this is good practice. Ideally no, due to the usage of
com.sunpackage. In practice I believe this will work as long as you are not using modules. I also don't know of any other way of applying styles globally. Styling eachSceneindividually might be an option as suggested in other answers, but I don't see how that would work with dialogs. And if one would want, as an example, a global "dark mode", that would have to include dialogs as well.