I try to create a table with javafx. The cell content is mostly short text or numbers but the column header is a long describing text. So I would prefer to rotate the text in the column header.
The code below works but the problem now is that the text in the header will be shortened (with ellipsis at the end) when the column width is reduced with the mouse.
Can this behaviour be 'switched off'? Any other proposals?
Here is my code:
public class TableRotHeader extends Application {
private static final Logger LOG = Logger.getLogger(TableRotHeader.class.getName());
@Override
public void start(Stage stage) throws IOException {
Group group = new Group();
Scene scene = new Scene(group);
stage.setTitle("Table with rotated header");
stage.setWidth(800);
stage.setHeight(600);
TableView tableView = new TableView();
TableColumn colA = new TableColumn("horizontal\ncol header");
TableColumn colB = new TableColumn("");
final int minWidth = 25;
colA.setMinWidth(minWidth);
colB.setMinWidth(minWidth);
MyLabel l = new MyLabel("labeled attribute");
l.setRotate(-90);
l.setPrefHeight(150);
colB.setGraphic(l);
tableView.getColumns().addAll(colA, colB);
group.getChildren().add(tableView);
tableView.prefWidthProperty().bind(scene.widthProperty());
tableView.prefHeightProperty().bind(scene.heightProperty());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

edit: both answers (from James_D and Mailkov) works great.
I now want to group 2 labels to get 2 vertical lines. This causes a distance between them.
A: How to get a reasonable distance (red arrow in screenshot)? This could be textheight * 1.2 or similar.
B: How to get a few pixels distance to the top and bottom of the labels (blue arrow)?
Label label1 = new Label("col 1");
label1.setRotate(-90);
MyLabel label2 = new MyLabel("col number 2 with a long text");
label2.setRotate(-90);
label2.setStyle("-fx-background-color: white; -fx-font-weight: normal");
Group g = new Group(label1, label2);
colB.setGraphic(g);

Wrap the label in a Group:
colB.setGraphic(new Group(l));
You can use setMinWidth() and setMinHeight() for MyLabel l
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With