首页 > 解决方案 > 在 JavaFX 中更改复选框树项的 CSS 样式

问题描述

我正在使用 JavaFX 8 并有一个treeView填充有复选框树项目,如图所示。我想应用 CSS 以便我可以:

  1. 更改复选框的背景颜色
  2. 更改复选框的边框颜色
  3. 更改复选框内选择图标的颜色

我到处搜索,但无法弄清楚如何做到这一点,并尝试了以下命令的许多变体,但未能使其正常工作。

.tree-view .check-box-tree-cell .check-box {
    -fx-background-color: black;
    -fx-border-color: orange
}

.tree-view .check-box {
    -fx-background-color: black;
    -fx-border-color: orange
}

.check-box-tree-cell {
    -fx-background-color: black;
    -fx-border-color: orange
}

.tree-cell .check-box {
    -fx-background-color: black;
    -fx-border-color: orange;
}

我该如何解决这个问题?

标签: javacssjavafxcheckboxtreeview

解决方案


试试这个:

.tree-view .check-box .box {
    -fx-background-color: lightskyblue;
    -fx-border-color: dodgerblue;
}

.tree-view .check-box:selected .box .mark,
.tree-view .check-box:indeterminate .box .mark {
    -fx-background-color: midnightblue;
}

如果您正在使用,这也将起作用CheckBoxTreeCell

.check-box-tree-cell > .check-box > .box {
    -fx-background-color: lightskyblue;
    -fx-border-color: dodgerblue;
}

.check-box-tree-cell > .check-box:selected > .box > .mark,
.check-box-tree-cell > .check-box:indeterminate > .box > .mark {
    -fx-background-color: midnightblue;
}

推荐阅读