Javafx How To not apply a css style to Label

Guido picture Guido · Jan 17, 2015 · Viewed 17k times · Source

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;
}

Answer

Roland picture Roland · Jan 17, 2015

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");