In my JavaFX project I am allowing users to set an accent color that will be used throughout the application. This accent color will be stored in a sqlite database.
Upon initialization, the database file will be read, and the accent color is applied. Currently this is done painfully by calling the .setStyle() method on every element that needs to be changed. This method is prone to mistakes and difficult to maintain. I am adding all elements that need the change applied to an array and looping through it, but it is easy to forget to add an element to the array every time I add a new button, etc. Not to mention different types of elements need different styles applied.
The only way I can think to accomplish this currently is to store the entire CSS element including accent color as a String, save the string to a .css file on the filesystem, and then load the external CSS. But this method is not graceful.
So I am aware of the following two ways to accomplish what I want, but both are not ideal.
- Call .setStyle() on every element, adding all elements to an array and looping
- Store my CSS in a String, save to the file system, load CSS, delete file from file system
Ideas like less, sass, and scss won't work because they require knowing what the color will be at build time, which I won't know. Having separate stylesheets prepared ahead of time won't work because there are millions of colors the user might choose.
An ideal answer would be some way to modify the add() method to accept css rules as a string (similar to how you pass them to .setStyle() method) instead of just a file path like this (example, does not work):
primaryStage.getScene().getStylesheets().add("-fx-background-color:" + getAccentColor() + ";");
Any ideas? Surely someone out there has provided user configurable accent colors before.
Thank you!
setStyle(...)setStyle("-fx-base: "+getAccentColor()+";")on the root node?