我有这个(简单的)fxml/css 和 java 的组合。有问题的组合框 (operationSelector) 确实被禁用,但它的不透明度是 0.4(禁用节点的默认值而不是 1.0)。打印 this.operationSelector.getStyleClass() 显示 singleton-combo-box 类确实是其样式类的一部分。我尝试禁用所有样式类但 singleton-combo-box 并且禁用时不透明度仍然为 0.4。有什么明显的错误跳出来了吗? (对了,我用的是Java 8u40抢先体验)
在 FXML 中:
<ComboBox fx:id="operationSelector" styleClass="singleton-combo-box" GridPane.columnIndex="1" GridPane.rowIndex="1" />
在 CSS 中:
.singleton-combo-box:disabled {
-fx-opacity: 1.0;
}
.singleton-combo-box .list-cell:disabled {
-fx-opacity: 1.0;
-fx-text-fill: black;
}
在 Java 中:
@FXML private ComboBox<String> operationSelector;
@FXML
private void initialize()
{
this.operationSelector.setDisable(true);
}
结果如下:
最佳答案
正如 eckig 所建议的,有一种使用私有(private) API (com.sun.*) 来执行此操作的方法:
StyleManager.getInstance().addUserAgentStylesheet(url)
经过一些研究后,我发现了 Jonathan Giles(Oracle JavaFX 团队的 UI 控件技术主管)的建议,他展示了一种无需直接使用私有(private) API 即可执行此操作的方法:
Application.setUserAgentStylesheet(url)
这种方法应该是为整个应用程序(所有阶段和场景)设置通用样式表的最佳方式。
关于JavaFX ComboBox 样式(禁用伪类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734213/