如何更改TableView行的颜色? [英] How to change the colors of the TableView's rows?
问题描述
我有一个表格视图,如果要...,我想更改行的默认颜色.
1. ...悬停在一行上方.
2. ...选择了一行.
I have a tableview and want to change the default colors of the rows if I...
1. ... hover above a row.
2. ... selected a row.
在css文件中是否有简单的方法来完成所有这些工作?
Is there a easy way to do all of it in the css-file?
我发现对于情况1,我可以设置
All I found out is that for case 1, I can set
.table-cell:hover {
-fx-background-color: green; }
.table-cell:hover {
-fx-background-color: green; }
.这样,单元格的颜色就会改变,但整个行的颜色不会改变.
in the css-file. With that the color of the cell changes but not the color of the whole row.
.table-view .row-selection:hover{
-fx-background-color: lightgreen; }
而且我尝试过的许多其他组合均无效.
.table-view .row-selection:hover{
-fx-background-color: lightgreen; }
and a lot of other combinations I tried are not working.
推荐答案
/* Selected row */
.table-view:focused .table-row-cell:filled:focused:selected {
-fx-background-color: brown;
-fx-background-insets: 0, 1, 2;
-fx-background: -fx-accent;
-fx-text-fill: -fx-selection-bar-text;
}
/* Selected row when table not focused */
.table-row-cell:filled:focused:selected {
-fx-background-color: red;
-fx-background-insets: 0, 1, 2;
-fx-background: -fx-accent;
-fx-text-fill: -fx-selection-bar-text;
}
/* Row hovered */
.table-view:row-selection .table-row-cell:filled:hover {
-fx-background-color: green;
-fx-background-insets: 0, 0 0 1 0;
-fx-text-fill: -fx-text-inner-color;
}
/* Selected row hovered */
.table-view:focused .table-row-cell:filled:focused:selected:hover {
-fx-background: -fx-accent;
-fx-background-color: yellow;
-fx-background-insets: 0, 1, 2;
-fx-text-fill: -fx-selection-bar-text;
}
/* Selected row hovered when table not focused */
.table-view:row-selection .table-row-cell:filled:focused:hover {
-fx-background-color: blue;
-fx-background-insets: 0, 0 0 1 0, 1 1 2 1, 2 2 3 2, 3 3 4 3;
-fx-text-fill: -fx-text-inner-color;
}
这篇关于如何更改TableView行的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!