Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx How To not apply a css style to Label

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;
}
like image 653
Guido Avatar asked Dec 20 '22 07:12

Guido


2 Answers

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");
like image 74
Roland Avatar answered Jan 16 '23 11:01

Roland


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.

like image 42
jewelsea Avatar answered Jan 16 '23 12:01

jewelsea