How can I rotate the label

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

How can i do this?


source to share

2 answers

Any node can have its CSS style rotation using the -fx-rotate

css attribute .

This is the angle of rotation in degrees. Zero degree is at 3 o'clock (right on the right). Angle values ​​are positive clockwise. Rotation around the center.

So, in your code or FXML, you can:



And in your CSS stylesheet, you can define:

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


Also notice the suggestion from James_D to wrap the label in the Batch to take rotation into account when performing layout bounds calculations.



Call setRotate

on a shortcut to rotate it centered.

To allow the layout panels to correctly measure the bounds of the label after rotation, wrap it in 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) {




All Articles