JavaFX - display background with disabled label

With JavaFX, I would like to display a label with a specific background color (when disabled, my Label background becomes transparent) and I tried the code below but it still doesn't work ... Do you have some advice for me? - Another thread told me to do setEditable(false)

, but this approach is not assigned 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);
}

      

+3


source to share


1 answer


You are experiencing this because the default attribute -fx-opacity

of controls :disabled

is set to 0.4

.

You can fix your selector as

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

      

which will show you disabled Label

with the required background color, but due to the removed opacity, the text color will be the same as on the shortcut without disabled .



A workaround could be to set the attribute -fx-text-fill

directly on the disabled selector, specifying the opacity with rgba

:

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

      

Here 's a 0.4 character rgba(170, 0, 0, 0.4)

RGB version #aa0000

.

Ref: JavaFX CSS Reference Manual : RGB Colors Section

+2


source







All Articles