Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display text vertically on a button in JavaFX?

Tags:

java

javafx

I want to create a button and display it's text vertically on it, in JavaFX. I am aware of rotate but I do not want to use it.Is there any other way to name the button vertically.enter image description here

like image 814
Rajesh Kumar Dash Avatar asked Sep 04 '13 09:09

Rajesh Kumar Dash


1 Answers

Rotate is the way to do this, there is no other solution.

rotated

Sample code:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/* Displays a button with it's text rotated 90 degrees to the left */
public class RotatedText extends Application {
    @Override public void start(Stage stage) {
        Button feedbackButton = createButtonWithRotatedText("Feedback Form");

        StackPane layout = new StackPane();
        layout.getChildren().setAll(feedbackButton);
        StackPane.setAlignment(feedbackButton, Pos.TOP_RIGHT);
        layout.setPrefSize(225, 275);
        layout.setStyle("-fx-background-color: lightcyan;");

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private Button createButtonWithRotatedText(String text) {
        Button button = new Button();
        Label  label  = new Label(text);
        label.setRotate(-90);
        button.setGraphic(new Group(label));

        // in-line css just used for sample purposes,
        // usually you would apply a stylesheet.
        button.setStyle(
                "-fx-base: orange; " +
                "-fx-font-size: 30px; " +
                "-fx-text-background-color: whitesmoke;"
        );

        return button;
    }

    public static void main(String[] args) { launch(args); }
}
like image 72
jewelsea Avatar answered Sep 19 '22 01:09

jewelsea