mimi
mimi

Reputation: 50

Set different size of button text lines, css, javafx

I made a button using scenebuilder which has two lines of text inside (first line is the function for button, and second one is a little explanation), and I would like to set different sizes for them, through css. Is this possible in any way, or is there more practical way for doing this?

Thank you for your help.

Upvotes: 2

Views: 1822

Answers (1)

fabian
fabian

Reputation: 82491

You cannot achieve this with a single text element like the one used for the button text, but you can add a Label as graphic and apply different text sizes via css:

@Override
public void start(Stage primaryStage) {
    Button btn = new Button("Text");
    Label explanation = new Label("This is the explanation");
    explanation.getStyleClass().add("explanation");
    btn.setGraphic(explanation);

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 300);
    scene.getStylesheets().add(getClass().getResource("explanation.css").toExternalForm());

    primaryStage.setScene(scene);
    primaryStage.show();
}

explanation.css

.button {
    -fx-font-size: 20;
    -fx-content-display: bottom;
}

.button>.explanation {
    -fx-font-size: 10;
}

Upvotes: 1

Related Questions