How to set several default CSS stylesheet for an application with JavaFX 9 (a replace for StyleManager)?
Asked Answered
G

1

7

Whose damn idea it was to exclude StyleManager without direct and clear replacement - it broke many programs as I see!

I need to make some restyling for the whole application (custom components, custom pseudo classes, custom platform-specific patches etc.). In JavaFX 8 I could use: com.sun.javafx.css.StyleManager.getInstance().addUserAgentStylesheet("MyShit.css");

But in Java 9 StyleManager is not available. So is there a way to set CSS for every scene?

Using "Application.setUserAgentStylesheet" is not an option because I don't want to lose default lookandfeel and I already call it to set MODENA style. I want to extend the default CSS not to replace it. And I don't want to rewrite the whole MODENA also (obviously).

And I really don't want to set the same stylesheet for every form/scene in the application manually (or add it to every FXML file). There many forms, dynamically constructed dialogs. And the main reason, there are parts (and libraries) which is shared between several applications so I don't want to hardcode any stylesheets (which might have different names and paths for different applications).

So I need a way to set an additional stylesheet for every possible scene of the application in one point of the program. Is that possible in FX9?

P.S. I looked into custom FXML loaders, CSS loaders, scene loaders and other ways to intercept scene creation - there is no way!

Gillis answered 13/11, 2017 at 22:1 Comment(3)
I don’t mean to sound heartless, but Java’s documentation has been warning for many years that com.sun.* packages were unsafe to use, because they would eventually go away. In Java 8, the compiler even emitted warnings for it. Perhaps you can create your own (possibly static) property for those shared UIs?Midnight
Shure they did warn, but they don't any alternatives.Gillis
Static property for what? For CSS file name? There are several shared GUI components and styles. Also I modify some styles of the basic stylesheet etc. Not I have to add local path to my CSS into all FXML, but it's project dependant, form location dependant and you silently loses this reference if you change the root component in FXML (coz it described in root component's tag). Also I have to add to many loaders and cinstruuctors for custom/modifieed dialogs etc. And If tomorrow (actually today) I need not one application wide CSS but two...And it also was an easy way to make custom skins.Gillis
B
2

It's just another lack of JavaFX - simply use the JVM parameter below to make the StyleManager accessible.

--add-exports=javafx.graphics/com.sun.javafx.css=ALL-UNNAMED
Bennettbenni answered 14/11, 2017 at 17:42 Comment(4)
This is just a temporary fix, and won't be continually supported past JDK 9Stripling
Well, it will work until JavaFX will be improved - means the StyleManager will be placed in a public package. Currently it's not clear when this will be.Bennettbenni
I don't believe there is a current road map to put anything else into the public packages based on the direction detailed at JavaOneStripling
There two ways to solve the problem. First, create a class named, for example StyleManagerProxy, where define the method addUserAgentStylesheet which just calls StyleManager.getInstance().addUserAgentStylesheet(). Compile it with the mentiond JVM parameter. And use this class. The second way is to create your own manager using Window().getWindows() . The class will listen when a window is added/removed and you can modify it's Scene with your Style Sheet.Centenary

© 2022 - 2024 — McMap. All rights reserved.