JavaFX/CSS:更改 ComboBox 所选项目的文本颜色

问题描述 投票:0回答:3

我正在使用一个名为 JFoenix 的自定义 JavaFX 库,它使用 Google 的 Material Design 彻底修改了一些 JavaFX 组件。我特别遇到的问题是在选择字符串组合框的选定项目后更改其文本颜色。 This is my before screen, with the item in question circled.

当我从组合框中选择一个项目时,文本从灰色变为黑色 (see screenshot here)。我希望所选项目中的文本与其余标签的颜色相同。在场景生成器中添加

-fx-text-fill
-fx-text-inner-color
不起作用。

我发现的唯一可能的解决方案是使组合框可编辑,并在用户选择选项后通过其编辑器设置颜色:

@FXML
private void handleComboBoxFormat() {
     this.mpaaBox.getEditor().setStyle("-fx-text-fill: #eceff1;" + "-fx-background-color: #445566");
}

我不喜欢这个解决方案,因为我不希望 ComboBox 可编辑,而且感觉很混乱。还有其他方法可以编辑文字颜色吗?谢谢!

java css javafx combobox scenebuilder
3个回答
3
投票

-fx-text-fill
未在组合框中定义。

但是阅读 CSS 参考指南 我看到了结构:

.combo-box > .list-view > .list-cell

其中

.list-cell
定义从 Labeled 继承的
-fx-text-fill


3
投票

好吧,在搞乱了 @MouseEvent 建议的 CSS 之后,我找到了解决方案。

当您打开弹出窗口时,

.combo-box .list-view .list-cell
会影响组合框列表中的项目。

.combo-box .list-cell
影响所选项目的标签,这是我访问时遇到问题的。


0
投票

非常感谢,这对我帮助很大。我又玩了一会儿,终于实现了这种造型。

使用这些条目:

/* outer combo-box text */
.combo-box .list-cell {
  -fx-text-fill: white;
}

/* combo-box list view text, not hovered or focused */
.combo-box .list-view .list-cell {
  -fx-text-fill: black;
}

/* combo-box selected text */
.combo-box .list-view .list-cell:focused {
  -fx-background-color : #5F9EA0;  /* Cadet Blue */
  -fx-text-fill: white;
}

/* combo-box hover text */
.combo-box .list-view .list-cell:hover {
  -fx-background-color : #5F9EA0;  /* Cadet Blue */
  -fx-text-fill: white;
}
© www.soinside.com 2019 - 2024. All rights reserved.