Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 2: word wrapping doesn't work in ScrollPane

Tags:

java

javafx-2

If I set word wrap to a label and put it in any layout - word wrapping works fine unless I put label to ScrollPane. Here is an example:

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Test");
    BorderPane borderPane = new BorderPane();
    VBox myView = new VBox();
    Label label = new Label("Lorem ipsum dolor sit amet, consectetur adipisicing elit," +
            " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim" +
            " ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip" +
            " ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate" +
            " velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat" +
            " cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum");
    label.setWrapText(true);
    myView.getChildren().addAll(label);

    ScrollPane scroll = new ScrollPane();
    scroll.setContent(myView);
    scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

    borderPane.setCenter(scroll);

    Scene scene = new Scene(borderPane, 300, 400);
    primaryStage.setResizable(false);
    primaryStage.setScene(scene);
    primaryStage.sizeToScene();
    primaryStage.show();
}

Is there any way to make word wrapping work inside scroll pane?

like image 490
Dragon Avatar asked Sep 07 '25 06:09

Dragon


1 Answers

Quoting the Javadoc :

public final void setWrapText(boolean value)

Property description:

If a run of text exceeds the width of the Labeled, then this variable indicates whether the text should wrap onto another line.

Well in your example as you did not set a size to your label it automatically sized to fit all the text. So the Label understood you wanted him to wrap the text but as the text fitted his width then he didn't do a thing.

Now, if you set a pref size for your label like that it will force him to wrap the text:

    label.setPrefSize(250, 500);

What you can do (and it's probably a better way) is tell your ScrollPane to resize components inside it using the setFitToWidth or setFitToHeight methods. Just like this :

    scroll.setFitToWidth(true); 

When you resize your window, the Label will be resized to fit the size the ScrollPane, and your text will be wrapped accordingly.

Another way to achieve what you want is using a Text Object instead of a Label :

Text text = new Text("Lorem ipsum dolor sit amet, consectetur adipisicing elit," +
            " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim" +
            " ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip" +
            " ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate" +
            " velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat" +
            " cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum");

    text.setWrappingWidth(250);
like image 51
Marc Avatar answered Sep 09 '25 23:09

Marc