4

With JavaFX, I would like to display a Label with a defined background color (when disabled, my Label's background becomes transparent), and I tried the code below, but that still doesn't work... Have you some tips for me? - Another thread told me to do a setEditable(false), but this approach isn't appropriated in my case.

FXML:

<Label styleClass="disable_backgrounded" layoutX="32.0" layoutY="23.0" prefHeight="25.0"
       style="-fx-background-color: rgb(252,252,252);-fx-padding: 5px;" text="General" textFill="#aa0000">
           <font>
                <Font name="System Bold" size="14.0"/>
           </font>
</Label>

CSS:

.disable_backgrounded:disabled {
    -fx-background-color: rgb(252,252,252);
}

1 Answers1

3

You are experiencing this, because the -fx-opacity attribute of the :disabled controls are set to 0.4 by default.

You can correct your selector as

.disabled-label:disabled {
    -fx-background-color: rgb(252,252,252);
    -fx-opacity: 1;
}

which will show you the disabled Label with the needed background color, but because of the removed opacity, the text color will be the same as on the non-disabled label.

A work-around can be to set the -fx-text-fill attribute directly for the disabled selector by defining the opacity using rgba:

.disabled-label:disabled {
    -fx-background-color: rgb(252,252,252);
    -fx-opacity: 1;
    -fx-text-fill: rgba(170, 0, 0, 0.4);
}

Here is rgba(170, 0, 0, 0.4) the RGB version of #aa0000 with 0.4 alpha defined.

Reference: JavaFX CSS Reference Guide: RGB Colors section

DVarga
  • 21,311
  • 6
  • 55
  • 60