Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 318
Sap Avatar asked Nov 13 '17 22:11

Sap


People also ask

Where do I put CSS in JavaFX?

The default css for all JavaFX applications is written in a file called modena. css , which can be found in the JavaFX runtime jar file, jfxt. jar , located in your Java installation folder. This css file defines the styles for the root node and the UI controls.

How do I add CSS files to Scene Builder?

In Scene Builder, you can simulate the attachment of a style sheet to an application Scene by selecting Preview, then Scene Style Sheets, and finally choosing Add a Style Sheet or Open a Style Sheet option. This Preview command is useful when the “root” style class is defined in the style sheet.


1 Answers

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
like image 63
wzberger Avatar answered Sep 21 '22 04:09

wzberger