TomJ
TomJ

Reputation: 1813

setWrapText(true) is not working for Label in JavaFX

I created a Label in JavaFX which has a contains a lot of text.

Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
            + "software to find both the C-Mark and monthly attendance "
            + "of students. Inorder to use the features of this software,"
            + " user has to create an account for him first. Then he should "
            + "login using the username and password. He will be able to "
            + "perform all the operations then. Further details are mentioned"
            + " in the 'HELP' section in the user home page.");
l1.setWrapText(true);
l1.setTextAlignment(TextAlignment.JUSTIFY);

In this code setWrapText(true) is not working. Why? How can I make it work?

Upvotes: 1

Views: 18932

Answers (3)

Ian
Ian

Reputation: 1

While the above solutions can work, one particular problem I ran into, is that if a Label is placed inside of a VBox, it will not wrap despite having the Labels max width set and wrapText is set to true. In this case, the Label must ALSO have its pref and minimum height set in order to begin wrapping, which can be annoying if you have multiple labels in the VBox and are trying to get them to work.

private VBox myCode() {
    VBox container = new VBox();
    int maxWidth = 200;
    container.setMaxWidth(maxWidth);

    Label one = new Label("This is very long");
    one.setMaxWidth(maxWidth);
    one.setPrefWidth(maxWidth);
    one.setWrapText(true);
    // Tailored to the text
    one.setMinHeight(100);
    one.setPrefHeight(100);

    Label two = new Label("This is also very long");
    two.setMaxWidth(maxWidth);
    two.setPrefWidth(maxWidth);
    two.setWrapText(true);
    // Tailored to the text
    two.setMinHeight(200);
    two.setPrefHeight(200);

    container.getChildren().addAll(one, two);
    return container;
}

To avoid annoyances involved with this, you use a TextFlow object instead of a label, and place one Text object into the TextFlow as it will wrap the text without needing to worry about what it's height should be.

private static TextFlow aLabel(String... pString) {
     TextFlow textFlowLabel = new TextFlow();
     textFlowLabel.setMaxWidth(MAX_WIDTH);
     textFlowLabel.setPrefWidth(MAX_WIDTH);
     textFlowLabel.setTextAlignment(TextAlignment.JUSTIFY);
     for (String aString : pString) {
         textFlowLabel.getChildren().add(new Text(aString));
     }
     return textFlowLabel;
}

private VBox myCode() {
     VBox container = new VBox();
     int maxWidth = 200;
     container.setMaxWidth(maxWidth);
     
     TextFlow labelOne = aLabel("This is very long");
     TextFlow labelTwo = aLabel("This is also very long");
     container.getChildren().addAll(labelOne, labelTwo);
     return container;
}

Upvotes: 0

Siphalor
Siphalor

Reputation: 723

I encountered the same problem in Scene Builder and the other solutions didn't work.

I finally got it working by setting the "Min Height" to USE_PREF_SIZE and keeping the "Pref Height" at USE_COMPUTED_SIZE.

This gets translated to FXML with the minHeight property set to -Infinity.

Upvotes: 2

James_D
James_D

Reputation: 209319

Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
                + "software to find both the C-Mark and monthly attendance "
                + "of students. Inorder to use the features of this software,"
                + " user has to create an account for him first. Then he should "
                + "login using the username and password. He will be able to "
                + "perform all the operations then. Further details are mentioned"
                + " in the 'HELP' section in the user home page.");
        l1.setWrapText(true);
        l1.setTextAlignment(TextAlignment.JUSTIFY);

Here is an SSCCE:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class WrappedLabelExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
                + "software to find both the C-Mark and monthly attendance "
                + "of students. Inorder to use the features of this software,"
                + " user has to create an account for him first. Then he should "
                + "login using the username and password. He will be able to "
                + "perform all the operations then. Further details are mentioned"
                + " in the 'HELP' section in the user home page.");
        l1.setWrapText(true);
        l1.setTextAlignment(TextAlignment.JUSTIFY);

        StackPane root = new StackPane(l1);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

which results in

enter image description here

Upvotes: 15

Related Questions