How can I dynamically add and remove css for an entire JavaFX application?

I am using JDK8 build 87 and want to dynamically add and remove css stylesheets so that they can be used by my entire JavaFX application.

I am currently setting the default styleSheet with this command:

 Application.setUserAgentStylesheet(Application.STYLESHEET_MODENA); 

and then when I want to add an extra CSS stylesheet, I do this:

 com.sun.javafx.css.StyleManager.getInstance.addUserAgentStylesheet(styleSheet); 

It works, but I have two problems. Firstly, it uses a private API , and secondly, there seems to be no way to remove it as soon as I am done with it (I use OSGI , so it is common for modules to enter and exit).

In early 2012, there was talk of porting the StyleManager to the public API , but I'm not sure if something happened.

Does anyone know of a public method to add styleSheets so that they apply to the whole JavaFX application? Also how would I remove them?

(I do not have privileges to create a new javafx-8 tag)

+8
java javafx-2 javafx-8
source share
1 answer

According to the global stylesheet for your GUI application :

 // load default global stylesheet Application.setUserAgentStylesheet(null); // add custom global stylesheet StyleManager.getInstance().addUserAgentStylesheet(AQUA_CSS_NAME); 

However, as Boomah points out, StyleManager.getInstance().addUserAgentStylesheet not part of the JavaFX API, so this method is really not recommended to use it directly from user code. In addition, it works only to add a global stylesheet, and not to delete such a stylesheet after adding a stylesheet.


A more adventurous one might create a patch to add the feature suggested by Boomah, changing the StyleManager code to support deleting global stylesheets and changing Application , to provide a public API for a new function that uses the updated StyleManager , and then send the patch to openjfx-dev for inclusion in the platform JavaFX


In the meantime, you can manually set your user stylesheet in each of the application scenes - this is a pain, but here you are.,.

+3
source share

All Articles