我的 .css 文件中有这样的表格单元格代码:
.table-cell-warn
{
-fx-background-color: aliceblue;
}
.table-cell-error
{
-fx-background-color: yellow;
}
我已将这些 css 类添加到特定的 TableCells 通过。 o.getStyleClass.add("table-cell-warn")
或 o.getStyleClass.add("table-cell-error")
但是,当我现在选择彩色 TableRow 时,它不会使用为所选 TableRow 指定的颜色(默认情况下为浅蓝色)。我尝试添加这样的代码:
.table-cell-warn:selected
{
-fx-background-color: #0096C9;
-fx-accent: #0096C9;
-fx-focus-color: #039ED3;
}
.table-cell-error:selected
{
-fx-background-color: #0096C9;
-fx-accent: #0096C9;
-fx-focus-color: #039ED3;
}
到 .css 文件,但它没有任何改变。我是否也必须更改我的 Java 代码中的某些内容?还是我走错了路。
最佳答案
TableView
处于“行选择模式”,这就是将 :selected
伪类添加到包含 的
。以下 CSS 应该可以工作:TableRow
的原因>表格单元格
/* for row selection mode */
.table-row-cell:selected .table-cell-warn,
.table-row-cell:selected .table-cell-error,
/* for cell selection mode */
.table-cell-warn:selected,
.table-cell-error:selected
{
-fx-background-color: #0096C9;
-fx-accent: #0096C9;
-fx-focus-color: #039ED3;
}
关于JavaFX-8 为选定的 TableRow 设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40528994/