DmitriyAntonov DmitriyAntonov - 1 year ago 138
CSS Question

Change color of TextField (JavaFX) without side effect

Code to change color

c.getStyleClass().add("required");


and in css

.required {
-fx-control-inner-background: red;
}


But context menu on that textField will have the same color.

enter image description here

How to solve it?

Answer Source

The rule

.required {
    -fx-control-inner-background: red;
}

sets -fx-control-inner-background for the selected element and it's decendants.

Therefore you have to change it back to the default value for decendants of the selected node where the effect is not desired. Since the context menu is treated as decendant, you need to change it for the context menu, e.g. by adding this rule to the css:

.required .context-menu {
    -fx-control-inner-background: derive(-fx-base,80%);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download