我有一个包含一些数据的组合框。
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();
}
}
如果我设置组合框禁用它会变灰但我需要将文本设置为黑色。 Google说我需要将不透明度设置为1.0。
box.setDisable(true);
box.setStyle("-fx-opacity: 1.0;");
没有任何反应。它也变灰了。
即使我将text-fill
属性设置为黑色,它也会变灰。
box.setDisable(true);
box.setStyle("-fx-opacity: 1.0; -fx-text-fill: black;");
会发生什么?如何更改已禁用组合框的文本颜色?
答案 0 :(得分:14)
disabled
属性从场景图节点级联到其子节点,因此组合框的所有子节点都可以有效地获取其:disabled
CSS样式。因此,例如,显示所选项目的Label
使用其disabled
样式,其不透明度设置为0.4。
要实现您的目标,请执行
.combo-box:disabled, .combo-box:disabled > * {
-fx-opacity: 1.0 ;
}
在外部CSS文件中。