How can I rotate a label


In JavaFX 8 I would like to specify the css to rotate a Label so that instead of the text going from left to right, it goes from bottom to top.

How can I do that?

9/10/2014 5:38:20 PM

Accepted Answer

Any node can have it's rotation styled via CSS using the -fx-rotate css attribute.

This is the angle of the rotation in degrees. Zero degrees is at 3 o'clock (directly to the right). Angle values are positive clockwise. Rotation is about the center.

So in your code or FXML you can have:


And in your css stylesheet you can define:

.vertical { -fx-rotate: -90; }

Also note James_D's answer suggestion of wrapping the label in a Group to account for the rotation when performing layout bounds calculations.

9/10/2014 6:58:59 PM

Call setRotate on the label to rotate it about its center.

To allow layout panes to properly measure the bounds of the label after rotation, wrap it in a Group:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class RotatedLabelTest extends Application {

    public void start(Stage primaryStage) {
        Label label1 = new Label("Hello");
        Label label2 = new Label("World");

        Group labelHolder = new Group(label1);

        HBox root = new HBox(5, labelHolder, label2);

        Scene scene = new Scene(root, 250, 150);


    public static void main(String[] args) {

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