Label text position


Question

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?

1
11
1/15/2017 11:02:34 PM

Accepted Answer

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);
    }
}
17
3/24/2014 1:20:40 PM

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 :-)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon