2012-10-05 70 views
5

我有一個tableview,並希望更改行的默認顏色,如果我...
1. ...懸停在一行上方。
2. ...選中一行。
如何更改TableView行的顏色?

是否有一種簡單的方法可以在css文件中完成所有操作?

我所發現的是,對1的情況下,我可以在CSS文件設置

.table-cell:hover { -fx-background-color: green; }

。由此,單元格的顏色發生變化,但不是整行的顏色。

.table-view .row-selection:hover{ -fx-background-color: lightgreen; } 和我嘗試過的很多其他組合都無法正常工作。

回答

16
/* 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; 
} 
+0

+1,雖然我會從第一部分刪除'focused',所以它讀取:'.table-view:focused .table-row-cell:filled:selected'。原因是多重選擇啓用時,它隻影響第一個選定的行。 –