Styling JavaFX Styled Elements

I have a combo box with some data.

public class Test extends Application {
    public static final String[] items = "One Two Three".split(" ");
    @Override
    public void start(Stage primaryStage) throws Exception {
        final ComboBox<String> box = new ComboBox<>(FXCollections.observableArrayList(items));
        box.getSelectionModel().selectFirst();

        primaryStage.setScene(new Scene(box));
        primaryStage.show();
    }
}

      

Enabled ComboBox

If I set the combo box it was gray, but I need to set the text to black. Google says that I need to set the transparency to 1.0.

box.setDisable(true);
box.setStyle("-fx-opacity: 1.0;");

      

And nothing happens. It is also gray in color.

enter image description here

Even if I set the property text-fill

to black , it won't be available either.

box.setDisable(true);
box.setStyle("-fx-opacity: 1.0; -fx-text-fill: black;");

      

enter image description here

What's happening? How do I change the text color of a disabled combo box?

+3


source to share


1 answer


The property disabled

cascades from the scene graph node to its children, so all the children of the combo box are effectively CSS styled :disabled

. So, for example, Label

displaying the selected item uses a style disabled

that has an opacity of 0.4.

To achieve what you want, do



.combo-box:disabled, .combo-box:disabled > * {
  -fx-opacity: 1.0 ;
}

      

in an external CSS file.

+7


source







All Articles