【问题标题】:Change CSS styling of a checkbox tree item in JavaFX在 JavaFX 中更改复选框树项的 CSS 样式
【发布时间】:2021-11-29 20:13:26
【问题描述】:

我正在使用 JavaFX 8 并有一个填充有复选框树项目的 treeView,如 picture 所示。我想应用 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;
}

我该如何解决这个问题?

【问题讨论】:

    标签: java css javafx checkbox treeview


    【解决方案1】:

    试试这个:

    .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;
    }
    

    【讨论】:

    • 感谢这几乎可以完美地工作,但我还必须添加“.check-box:indeterminate .box .mark”以确保复选框的标记在只有一些父母孩子时正确着色节点被选中,否则默认的白色破折号出现在父节点的框中。
    • @rak 你是对的。我更新了我的答案以涵盖indeterminate state。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 2019-02-07
    • 2017-05-17
    • 2023-03-20
    • 1970-01-01
    • 2018-09-17
    相关资源
    最近更新 更多