Wrapping Label text in a VBox using FXML
Asked Answered
B

2

6

I'm writing a JavaFX application and I'd like to create a screen that contains 2 long pieces of text. I don't know what the text is ahead of time, it will be filled in by some code at run time.

To do this I thought I'd make a VBox with 2 Labels. I assume that if I don't add dimensions, the Labels will span the VBox. Since the text is long I'd like it to wrap.

Here's the FXML that I tried:

<VBox spacing="20" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="label1" text="Dummy Text" wrapText="true" />   
      <Label fx:id="label2" text="Dummy Text" wrapText="true" />  
   </children>
</VBox>

This doesn't work because the text doesn't wrap but simply goes off the right side of the window. What do I need to get this to work?

Bulwark answered 31/3, 2016 at 22:27 Comment(0)
A
8

Try to set prefWidth and prefHeight , since your label doesnt know when to start wrap

<VBox prefHeight="100.0" prefWidth="300.0"  ...

If that does not work try it on parent contrainer of HBox. Are you using scene builder? I recommend you to use it

Works well(its auto generated):

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="333.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <VBox prefHeight="200.0" prefWidth="325.0" BorderPane.alignment="CENTER">
         <children>
            <Label text="Wrap text Wrap text Wrap text Wrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap textWrap text" wrapText="true" />
            <Label layoutX="10.0" layoutY="10.0" text="just textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust textjust text" />
         </children>
      </VBox>
   </center>
</BorderPane>
Adebayo answered 31/3, 2016 at 22:56 Comment(3)
I'm not sure I understand why this works, but it does. Thanks.Bulwark
@SanderSmith, That sums up my experience with JavaFX.Edict
The most important thing to take away from this is probably the wrapText="true"-property.Eli
P
2

Did you use stylesheets there with class name "label1" and "label2" if you did, setting wrapText="true" will not work there in the fxml code

  1. You try to remove the id's and see if it wraps while wrapText="true" is still set

or

  1. Put some css code to that css file that would wrap text like this -fx-word-wrap: break-word; in the particular class;
Paphian answered 21/3, 2018 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.