Get style properties

David Grieve david.grieve at
Tue Jun 18 18:10:20 PDT 2013

On Jun 18, 2013, at 3:54 PM, Mario Torre <neugens at> wrote:

> Hi all!
> I was trying to figure out how JavaFX parses and uses the style under
> the hood and I stumbled on something I don't fully understand (I admit
> I'm not really a CSS expert yet :).
> In the modena.css, right at the .root selector we have:
>    /* These are needed for Popup */
>    -fx-background-color: inherit;
>    -fx-background-radius: inherit;
>    -fx-background-insets: inherit;
>    -fx-padding: inherit;
> and then, few lines below:
>    -fx-background-color: -fx-background;
> Is this intentional? How does JavaFX understand which value to choose?

I've got a sticky note on my desk reminding me to take a look at those "inherit" lines. They were needed at one point in time, but I'm not sure they are needed now. I guess I should enter that sticky note into JIRA!

> It seems (but I'm far from discovering the trick) that both values are
> preserved, but how does the system know which one to use?

CSS uses cascade rules to determine which to use. In the case of -fx-background-color, the second declaration will be used. In the modena theme,  if the root node of the scene is a Region, then the background-color of the Region will be whatever -fx-background is. 

> I assume that "inherit" is what is doing the magic here, probably by
> delegating to the closest matching declaration, but it's still not clear
> to me how everything is resolved.

In the case of the others (radius, insets, padding), if the root node is a Region, then css will try to find a parent with that style. This doesn't do much in the case of the root of a Scene since the root node doesn't have a parent. In the case of a popup, the parent of the root can be considered to be the ownerNode and these styles are intended to make it such that the background-radius, fill, insets and padding of the popup match those of the ownerNode (if there is one). That is the intention, but whether these actually work, I can't say for certain. And whether they are actually needed is another question. 

> Also, is there a way to retrive fully resolved values? I would like
> something like this, basically:
> Button.getStyle("-fx-backgroud-color").getValue();
> Or such.

There is, but not through public API. You can call List<Style> Node.impl_getMatchingStyles(CssMetaData cssMetaData, Styleable styleable). For example

Button button = new Button("Button");
CssMetaData fillMetaData = ((StyleableProperty)button.fillProperty).getCssMetaData();
List<Style> styles = Node.impl_getMatchingStyles(fillMetaData, button);

This is the API that SceneBuilder uses and has a JIRA against it to make it (or something similar) public.
> I found a way to match declared styles and basically get a StyleMap, but
> this uses internal API I wanted to check if I'm missing some obvious
> way, perhaps?
> Cheers,
> Mario

More information about the openjfx-dev mailing list