Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
}
like image 535
DOM Avatar asked May 10 '14 03:05

DOM


2 Answers

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.

like image 189
void256 Avatar answered Nov 08 '22 14:11

void256


I stuggled with this myself, but found this solution. This will right align a single column whereas the rest of the columns in the same table are left aligned:

In code:

myCol.getStyleClass().add("rightAlignedTableColumnHeader");

In CSS:

.column-header.rightAlignedTableColumnHeader > .label {
    -fx-alignment: center-right;
}
like image 39
s101 Avatar answered Nov 08 '22 14:11

s101