Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX disabled element styling

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?

like image 553
Ivan Avatar asked May 18 '15 20:05

Ivan


1 Answers

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.

like image 109
James_D Avatar answered Nov 24 '22 20:11

James_D