JavaFX disabled element styling

12,959

The disabled property cascades from a scene graph node to its child nodes, so all the child nodes of the combo box effectively pick up their :disabled CSS styles. So, for example, the Label displaying the selected item uses its disabled style, which has opacity set to 0.4.

To achieve what you want, do

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

in an external CSS file.

Share:
12,959
Ivan
Author by

Ivan

Just a developer.

Updated on August 05, 2022

Comments

  • Ivan
    Ivan almost 2 years

    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 combo box disabled it grayed but I need to set text black. Google says what I need to set opacity to 1.0.

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

    And nothing happens. It also grayed.

    enter image description here

    Even if I set text-fill property to black it also grayed.

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

    enter image description here

    What happens? How do I change text color of disabled combo box?