Label text position
Asked Answered
A

3

14

I have a Label with an image and text

final Label label = new Label(labelText);
label.setTextAlignment(TextAlignment.CENTER);

ImageView livePerformIcon = new ImageView(MainApp.class.getResource("/images/Folder-icon.png").toExternalForm());
label.setGraphic(livePerformIcon);

I get this as a visual result:

enter image description here

How I can change the text position? I want to set the text below the Image?

Avon answered 24/3, 2014 at 12:40 Comment(1)
use HBox or Anchor PaneHanks
D
21
label.setContentDisplay(ContentDisplay.TOP);

Play with this to see the effect of the different alignment settings:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class LabelGraphicAlignmentTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Label label = new Label("Some\ntext");
        label.setGraphic(new ImageView(getClass().getResource("/images/Folder-icon.png").toExternalForm()));
        label.setMaxWidth(Double.POSITIVE_INFINITY);
        label.setMaxHeight(Double.POSITIVE_INFINITY);
        label.setStyle("-fx-border-color: blue;");
        root.setCenter(label);

        ComboBox<ContentDisplay> contentDisplayBox = new ComboBox<>();
        contentDisplayBox.getItems().addAll(ContentDisplay.values());
        contentDisplayBox.getSelectionModel().select(ContentDisplay.LEFT);
        label.contentDisplayProperty().bind(contentDisplayBox.valueProperty());

        ComboBox<Pos> alignmentBox = new ComboBox<>();
        alignmentBox.getItems().addAll(Pos.values());
        alignmentBox.getSelectionModel().select(Pos.CENTER);
        label.alignmentProperty().bind(alignmentBox.valueProperty());

        ComboBox<TextAlignment> textAlignmentBox = new ComboBox<>();
        textAlignmentBox.getItems().addAll(TextAlignment.values());
        textAlignmentBox.getSelectionModel().select(TextAlignment.LEFT);
        label.textAlignmentProperty().bind(textAlignmentBox.valueProperty());

        GridPane ctrls = new GridPane();
        ctrls.setHgap(5);
        ctrls.setVgap(5);
        ctrls.setPadding(new Insets(10));

        ctrls.addRow(0, new Label("Content display:"), new Label("Alignment:"), new Label("Text Alignment:"));
        ctrls.addRow(1,  contentDisplayBox, alignmentBox, textAlignmentBox);

        root.setTop(ctrls);

        Scene scene = new Scene(root, 600, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Dwanadwane answered 24/3, 2014 at 13:5 Comment(0)
P
15

I had to center the text of a label which acted like a title. The following code snippet did the trick.

        final Label title = new Label("Some text");
        title.setMaxWidth(Double.MAX_VALUE);
        title.setAlignment(Pos.CENTER);

Good programming :-)

Pathogenesis answered 12/5, 2016 at 7:57 Comment(0)
T
1

I was able to grab the button from the fxml file:

@FXML Label countDownClockLabel;

Then when the controller was initialized i set the text position:

@FXML


@Override
    public void initialize(URL arg0, ResourceBundle arg1) {

    countDownClockLabel.setAlignment(Pos.CENTER);
    }

You have to import:

import javafx.geometry.Pos;
Thornton answered 11/12, 2020 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.