JavaFX禁用了元素样式

时间:2015-05-18 20:32:46

标签: css combobox javafx

我有一个包含一些数据的组合框。

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

如果我设置组合框禁用它会变灰但我需要将文本设置为黑色。 Google说我需要将不透明度设置为1.0。

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

没有任何反应。它也变灰了。

enter image description here

即使我将text-fill属性设置为黑色,它也会变灰。

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

enter image description here

会发生什么?如何更改已禁用组合框的文本颜色?

1 个答案:

答案 0 :(得分:14)

disabled属性从场景图节点级联到其子节点,因此组合框的所有子节点都可以有效地获取其:disabled CSS样式。因此,例如,显示所选项目的Label使用其disabled样式,其不透明度设置为0.4。

要实现您的目标,请执行

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

在外部CSS文件中。