JavaFX, TableView and the font color of the selected row


Question

I get TableView like this:

TableView tableView = (TableView) ((AnchorPane) node).getChildren().get(0);

fill it with data like

tableView.setItems(FXCollections.observableList(vehicles));

this part is Ok. However, I'd like to change font properties of the currently row in the tableView and I cannot figure how to do it

I do not want to set tableView.getSelectionModel().setCellSelectionEnabled(true); as I want to display selection for the whole row, and not for a single cell

I tried to apply some css styles on TableRow and on Tabliew but without any success.

Is there any way to change font properties for the currently selected row in TableView?

1
0
8/6/2013 5:34:24 AM

Accepted Answer

Tableview css styling is subtle and complicated.

Solution

Go to the source for the default JavaFX controls css and copy all of the .table-view sections which have a :selected psuedo-class into your own user stylesheet.

Remove the default attribute setting rules from each css selector section you copied and replace them with the css you want to apply for your selected rows.

As there of lots of :selected rules you might have to copy the same attribute settings in a few places (similarly to how the default attribute values are defined).

Background Resources

The default stylesheets for respective versions are:

Consult the css reference guide and css tutorials if your are confused.

Quick Color Change Fix

Note that if all you need is to set the fill color for the selection bar text, you can override the default constant color with your own constant using a single statement:

.root { -fx-selection-bar-text: firebrick; }

Just substitute whatever color you want for firebrick. Similarly -fx-selection-bar: midnightblue; can be used to change the color of the background of the text.

5
8/6/2013 1:22:16 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon