ImpulseTheFox ImpulseTheFox - 29 days ago 5
CSS Question

JavaFX-8 set color for selected TableRow

I have code like this for tablecells in my .css file:

.table-cell-warn
{
-fx-background-color: aliceblue;
}

.table-cell-error
{
-fx-background-color: yellow;
}


I have added those css classes to specific TableCells via.
o.getStyleClass.add("table-cell-warn")
or
o.getStyleClass.add("table-cell-error")


But when I select a colored TableRow now, it does not use the colors specified for selected TableRows (by default a light blue). I tried adding code like this:

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


to the .css file, but it changed nothing. Do I have to change something in my java code, too? Or am I on the wrong path.

Answer

The TableView is in "row selection mode", which is why the :selected pseudoclass is added to the TableRow containing the TableCell. The following css should work:

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