Where can I find a complete official reference on JavaFX CSS?

血红的双手。 提交于 2021-01-28 04:05:54

问题


When I want to change the background colour of my TextArea in JavaFX (with CSS), many people recommend "-fx-control-inner-background". But when I look up the Oracle's CSS reference there's no such a thing as "-fx-control-inner-background"! In fact nowhere, in the Internet I could find a reference on that and yet it works!

My question is where do these people discover this information? Why, for example, isn't -fx-background-color working instead?


回答1:


Note that -fx-control-inner-background is not a CSS property, but a "looked-up color" (essentially a CSS variable) that is defined in the default stylesheet, modena.css.

To the best of my knowledge, there's no official documentation that describes the looked-up colors used by modena. The only resource I know for finding these is the source code, which quite carefully documents the purpose and use of these variables.

It is valid to be concerned about back-compatibility when relying on undocumented functionality. I'd make the following arguments, that alleviate this concern to some extent:

  • The design of modena.css makes it quite clear the intention is that the looked-up colors defined in there are intended to be a mechanism for easily theming an application, and as such are written as though they are an API
  • Using these looked-up colors is in widespread use in the JavaFX programming community, and removing them from modena.css in a subsequent version would break a lot of code and be met with substantial opposition from the community. As such, these form a "de-facto API".
  • The call

    setUserAgentStylesheet(Application.STYLESHEET_MODENA);
    

    in the Application subclass will ensure that modena is used as the default stylesheet, so if a new default stylesheet is defined for future JavaFX releases, this code will future-proof your application, under the "de-facto API" assumption in the previous bullet point. (Note this also gives the JavaFX team a way forward to create a new stylesheet without breaking existing code, which I think strengthens the "de-facto API" argument.)

So, on balance, I think relying on the looked-up colors that you can find in the modena.css source code is a safe approach.




回答2:


I finally found out something useful that shifts my understanding of FXML CSS.

According to this reference, each node in the scene graph can have substructures. For example in our case, TextArea has a substructure called content. It's this content that we should be looking to change and not the entire textarea. In this case:

.text-area .content { -fx-background-color: red; }

works perfectly for me. In other words, we have a region (called content) drawn on the main node (textarea).

This also shows why many people would get only a, for example, red border when they try to change the background of an textarea to red. It's because that narrow border is the only part beneath the content visible to us.

Hope this helps ...



来源:https://stackoverflow.com/questions/62472102/where-can-i-find-a-complete-official-reference-on-javafx-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!