20467

How can I rotate a label

Question:

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?

Answer1:

Any node can have it's rotation styled via CSS using the <a href="http://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#node" rel="nofollow">-fx-rotate</a> css attribute.

<blockquote>

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.

</blockquote>

So in your code or FXML you can have:

label.setStyle("vertical");

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.

Answer2:

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 { @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); } }

Recommend

  • Difference between initialized controllers and FXML linked controllers?
  • The table cells are empty in my tableview. JavaFX + Scenebuilder
  • JavaFX 8 Spinner control doesn't update value that's displayed
  • Python - Shoot a bullet in the direction (angle in degrees) my spaceship is facing
  • UIBezierPath not updated as expected
  • FLAG_SECURE not working on DialogFragment with style as DialogFragment.STYLE_NO_TITLE
  • Android Canvas drawLine not drawing on MainActivity
  • wrap content in JavaFX
  • Three js, get the vector3 that pointerlockcontrolsi s facing
  • How do I make a continuous loop with the animate method?
  • How to get phone heading for augmented reality?
  • JavaFX: Align Buttons inside ButtonBar (using SceneBuilder or fxml)
  • PictureBox zoom and scroll on mouse wheel C#
  • Animating an SVG Group
  • NetBeans doesn't see style.css [duplicate]
  • How to add regEx in angular filter
  • my tic-tac-toe program in matlab does not work [closed]
  • C# List of Panels
  • Plotting densities in R
  • Update Google Maps traffic layer without page reloading
  • xcode don't localize specific strings
  • Adjust width of select element according to selected option's width
  • C: Incompatible pointer type initializing
  • Installing iPhone App to iPhone
  • Swift: Switch statement fallthrough behavior
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Jquery UI tool tip close icon
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • What is the “return” in scheme?
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • how to add data labels for bar graph in matlab
  • Acquiring multiple attributes from .xml file in c#
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal