How can I rotate the label
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:
label.setStyle("vertical");
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.
+6
source to share
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 {
@Override
public void start(Stage primaryStage) {
Label label1 = new Label("Hello");
Label label2 = new Label("World");
label1.setRotate(-90);
Group labelHolder = new Group(label1);
HBox root = new HBox(5, labelHolder, label2);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 250, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
+3
source to share