2 Replies Latest reply: Nov 19, 2012 12:54 AM by neilghosh RSS

    Wrapping JavaFx Text Component

      I am using Text component to display some text looks like there is no way to automatic wrapping like label component. I only found

      text.setWrappingWidth(double d)

      But this needs a number as a constraint. I have some other component next to the text element whose size is dynamic so I can't give a fixed number to limit the text wrap. Is there any way to automatically wrap if the space is not enough ?

      Note : I am using Text instead of label because it honors \n character for deliberate next line wrapping but it is not possible to go through the whole text file and make sure it has a \n so that it wraps to next line.
        • 1. Re: Wrapping JavaFx Text Component
          Label also recognizes the \n character.
          import javafx.application.Application;
          import javafx.scene.Scene;
          import javafx.scene.control.LabelBuilder;
          import javafx.scene.layout.BorderPane;
          import javafx.stage.Stage;
          public class LabelTest extends Application {
            public void start(Stage primaryStage) throws Exception {
              BorderPane root = new BorderPane();
             root.setCenter( LabelBuilder.create()
                  .text("This is line 1.\n\nThis is line 2. "
                      +"If there is not enough horizontal space "
                      +"this line will wrap.")
                  .build() );
             Scene scene = new Scene(root, 400, 400);
            public static void main(String[] args) {launch(args);}
          • 2. Re: Wrapping JavaFx Text Component
            Thank you so much James !
            The whole reason why I refactored my code from Label to Text was to enable \n. Now I have to restore my original code.

            Actually when I did the \n experiment , I put the \n character in the text file and it displayed as is. So I thought label does not support \n.
            However if I add the \n character after reading the file in memory , a new lines comes up while rendering. So basically I have to probably search and replace "\\n" with "\n"

            Anyway this solves my problem as of now, but I have a feeling I may require some more features like effects in future where I would need Text component to be wrapped.