在 JavaFX 中更改树单元格选定的字体颜色

Change tree-cell selected font color in JavaFX

我有一个 JavaFX TreeView 并且想将选定单元格的字体颜色更改为黑色,使其看起来像一个未选定的单元格。 (我试过 setSelectionModel(null) 但这会引发错误。)单元格在背景图像上具有透明背景,如果这很重要的话。

CSS:

.tree-view, .tree-cell {
    -fx-font: 20px "Segoe Print";
    -fx-background-color: transparent;
}

.tree-cell {
    -fx-background-color: transparent;
    -fx-padding: 0 0 0 0;
    -fx-text-fill: #000000;
}

.tree-cell:focused {
    -fx-text-fill: #000000;
}

.tree-cell:selected {
    -fx-text-fill: #000000;
}

.tree-cell .tree-disclosure-node {
    -fx-background-color: transparent;
    -fx-padding: 10 10 0 40;
}

.button {
    -fx-padding: 0 10 0 10;
}

结果:("test4"被选中)

这似乎应该将选定单元格的字体颜色设置为黑色,但它并没有发生。 CSS 文件已加载并正在使用(例如,字体是正确的,我也修改了其他内容),所以这不是问题。

设置单元格的背景颜色:

 .myTree .tree-cell {
  -fx-background-color: #0a0a0a ;
  -fx-text-fill: #ffffff ;
  }  

如果您只是想要透明背景并且让选中的单元格与未选中的单元格显示相同,您可以这样做:

.tree-view {
    -fx-text-background-color: black ;
    -fx-background-color: transparent ;
    -fx-selection-bar: transparent ;
}