JavaFx how to align only one column header in tableview?

How to align the header of only one column in a tableview?
The following css aligns the entire column header, but I want to align only one column:

.table-view .column-header .label{
    -fx-alignment:CENTER
}
+4
source share
4 answers

You can do this now with the latest versions of JavaFX.

Java 1.7

This worked for me with Java: 1.7.0_45 / JavaFX: 2.2.45-b18). This must be the latest version because it requires RT-14909

"Id", TableColumn! TableColumn (s) (CSS-), Scenebuilder TableColumn , , tableColumn.setId("my-special-column").

Id:

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

PS: , . , !

Java 8

, , id Java 8. , , styleClass ( TableColumnHeader) Class css:

Java:

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

CSS:

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

jdk1.8.0_05 Mac OS X.

+11

, . , :

:

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

CSS:

.column-header.rightAlignedTableColumnHeader > .label {
    -fx-alignment: center-right;
}
+1

@DOM @void256 , , , . , , , CENTER_RIGHT . jre 1.8.0_45 eclipse luna , Scene Builder, ,

public void initialize(URL arg0, ResourceBundle arg1) {
    firstNameColumn.getStyleClass().add("top-left");
    lastNameColumn.getStyleClass().add("any-this-works-here;");
    phoneNumberColumn.getStyleClass().add("top-left;");
    emailAddressColumn.getStyleClass().add("top-left;");
}


.table-view .column-header .label{
    -fx-font-size: 12pt;
    -fx-text-fill: blue;
    -fx-alignment:top-left;

}
0

( , ). jdk1.8.0_66 Windows.

, :

// Hack: align column headers to the center.  
table.widthProperty().addListener((src, o, n) -> Platform.runLater(() -> {
  if (o != null && o.intValue() > 0) return; // already aligned
  for (Node node: table.lookupAll(".column-header > .label")) {
    if (node instanceof Label) ((Label)node).setAlignment(Pos.CENTER);
  }    
}));
0

Source: https://habr.com/ru/post/1539998/


All Articles