Javafx How To not apply a css style to Label

18,864

Solution 1

You could remove the label style alltogether with

lineCommentColorLabel.getStyleClass().remove("label");

and apply appropriate styles. Or you could add a styleclass to the other labels except the ones you wish to colorize manually like this:

.myclass
{
    -fx-text-fill: red;
}

and

lineCommentColorLabel.getStyleClass().add("myclass");

Solution 2

Background Info

There is some information on preference in the JavaFX CSS reference guide:

a style from a user agent style sheet has lower priority than a value set from code, which has lower priority than a Scene or Parent style sheet. Inline styles have highest precedence. Style sheets from a Parent instance are considered to be more specific than those styles from Scene style sheets.

Note also that an application only has a single user agent stylesheet which is generally the built-in standard stylesheet for JavaFX (e.g. modena.css for JavaFX 8). Usually applications do not override this stylesheet.

So, your scene stylesheet which sets the text fill of the label will have preference over inline code which sets the text fill of the label - and the css rule will take effect whenever the internal JavaFX implementation decides to applyCss to the scene graph.

Potential Solutions

Binding

A hacky way to work around this is the following statement:

customizedLabel.textFillProperty().bind(colorPicker.valueProperty());

This works because a bound value cannot be set. Once you do a bind, the CSS implementation cannot reset the label fill to another value.

Take care with your style rules

Another way to handle it is to ensure that you don't set -fx-text-fill in your scene stylesheet for the labels that you want to customize the text fill of in code. That is essentially, Roland's "myclass" or remove "label" styleclass solution.

Modify the the UserAgent stylesheet

You could also copy the standard JavaFX stylesheet, modify it directly, and set it on your application using setUserAgentStylesheet, then you can override in code any css settings made by the user agent stylesheet. I'd only consider this is you are deploying to a controlled JavaFX runtime (e.g. as a self-contained application deployment) and are OK to merge in changes to the user agent stylesheet whenever you update the runtime.

Share:
18,864
Guido
Author by

Guido

Updated on June 25, 2022

Comments

  • Guido
    Guido almost 2 years

    in my Java GUI I have about 50 labels. All of them will be formatted using CSS style. But there are 5 for which I want to change the font color using a ColorPicker like this:

    Color ch = lineCommentColorPicker.getValue();
    if (ch != null) {
        lineCommentColorLabel.setTextFill(ch);
    }
    

    My question. How can I avoid that the color I set using the setTextFill method will be overwritten again by CSS style rules defined for the class ".label"

    .label {
        -fx-text-fill: black;
        -fx-font-weight: bold;
        -fx-font-size: 12;
    }