JavaFx how to align only one column-header in tableview?

java, javafx, javafx-8, javafx-css

Solution

You can do that now with recent versions of JavaFX.

Java 1.7

This worked for me with Java: 1.7.0_45 / JavaFX: 2.2.45-b18). It should be a recent version because it requires RT-14909

The column-header will automatically get the same "Id" as the TableColumn! Therefore your TableColumn(s) need an (CSS-) Id that you can either set in Scenebuilder for the TableColumn directly or use some code like `tableColumn.setId("my-special-column")`.

Then you can style the column-header directly using the Id:

.table-view .column-header#my-special-column .label {
  -fx-alignment: CENTER_RIGHT;
  -fx-text-fill: red;
}

PS: This took me some frustrating 1,5 hours to figure out. Hope this helps others now!

Java 8

For reasons that I don't understand the id trick does not work for Java 8 anymore. However, what we can do is to set a styleClass for the column directly (which internally is propagated to the TableColumnHeader) and change this styleClass in the css file:

In Java:

firstNameCol.getStyleClass().add("my-special-column-style");

And in CSS:

.my-special-column-style .label {
  -fx-alignment: CENTER_RIGHT;
  -fx-text-fill: red;
}

Works for me here with jdk1.8.0_05 on Mac OS X.

Problem

How to align title of only one column in tableview? The following css aligns all column-header but I want to align just one column: ``` .table-view .column-header .label{ -fx-alignment:CENTER } ```

Original source