Radu Stefan Popescu
Radu Stefan Popescu

Reputation: 197

How to make JavaFX text wrap work?

I can't for the life of me make JavaFX wrap text. Instead my boxes get the size of the Text.

This is my main:

BorderPane main = FXMLLoader.load(getClass().getResource("main.fxml"));

primaryStage.setTitle("D394");
primaryStage.setScene(new Scene(main, 1000, 500));
primaryStage.getScene().getStylesheets().add("main.css");
primaryStage.show();

And this is my main.fxml. It's pretty big, sorry for that.

<BorderPane prefHeight="401.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.112-ea"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="ro.softvisionmedia.gui.MainController">
    <top>
        <MenuBar fx:id="menuBar" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity">
            <Menu items="" text="Lala">
            </Menu>
        </MenuBar>
    </top>

    <center>
        <TabPane fx:id="tabPane">

            <Tab id="tab_1" fx:id="tab1_D394" closable="false" text="D394">
            </Tab>

            <Tab fx:id="tab2_DateIdentif" closable="false" text="Date identificare">
                <TabPane fx:id="tabPane_dateIdentif">
                    <Tab fx:id="identifPersTVA" closable="false" text="Persoana inregistrata in scopuri de TVA">
                    </Tab>

                    <Tab fx:id="identifRepFiscal" closable="false" text="Reprezentant fiscal/legal/imputernicit">
                    </Tab>
                    <Tab fx:id="identifIntocmit" closable="false" text="Intocmitor">
                        <VBox fx:id="vBoxIntocmitor">
                            <HBox>
                                <TextFlow>
                                    <Text text="Subsemnatul "/>
                                    <Text fx:id="denumireRtext"/>
                                    <Text text=", in calitate de reprezentant legal, "/>
                                    <TextField fx:id="functieReprez"/>
                                    <Text>declar, sub
                                        sanctiunile aplicate faptei de fals in acte publice, ca datele din aceasta
                                        declaratie sunt corecte si complete, iar persoana care a intocmit prezenta
                                        declaratie este
                                    </Text>
                                </TextFlow>
                            </HBox>
                            <HBox>
                                <Label text="Denumire/nume prenume"/>
                                <TextField fx:id="denumireIntocmitor"/>
                                <Label text="CUI/CNP/NIF"/>
                                <TextField fx:id="cifIntocmitor"/>
                            </HBox>
                            <HBox>
                                <Label text="Persoana juridica"/>
                                <RadioButton fx:id="tipIntocmitorPJ"/>
                                <Label text="Calitate"/>
                                <TextField fx:id="calitateIntocmitor"/>
                            </HBox>
                            <HBox>
                                <Label text="Persoana fizica"/>
                                <RadioButton fx:id="tipIntocmitorPF"/>
                                <Label text="Functia in cadrul persoanei impozabile"/>
                                <TextField fx:id="functieIntocmitor"/>
                                <Label text="Alta calitate"/>
                                <Text text="?????"/>
                                <!-- @todo ce vine aici? -->
                            </HBox>
                            <HBox>
                                <TextFlow>
                                    <Text text="De asemenea, subsemnatul "/>
                                    <Text fx:id="denumireRtext2"/>
                                    <Text text=",in calitate de reprezentant legal "/>
                                    <Text fx:id="functieReprezText"/>
                                    <Text text=" declar:"/>
                                </TextFlow>
                            </HBox>
                            <HBox>
                                <Text>1. sunt de acord ca, pentru anul fiscal 2016, in sensul prevederilor art. 11 alin
                                    (3) lit. d) din Legea nr. 207/2015 privind Codul de procedura fiscala, datele
                                    inscrise in
                                    prezenta declaratie referitoare la tranzactiile derulate cu fiecare persoana
                                    impozabila (client/furnizor) inregistrata in scopuri de TVA sa fie consultate de
                                    catre aceasta
                                    prin intermediul aplicatiei informatice pusa la dispozitie de ANAF.
                                </Text>
                                <RadioButton fx:id="optinueDA" text="Da"/>
                                <RadioButton fx:id="optiuneNU" text="Nu"/>
                            </HBox>
                            <HBox>
                                <Text>2. Sunt de acord cu schimbarea optiunii , astfel ca pentru anul fiscal 2016, in
                                    sensul prevederilor art. 11 alin (3) lit. d) din Legea nr. 207/2015 privind Codul de
                                    procedura fiscala, datele inscrise in prezenta declaratie referitoare la
                                    tranzactiile derulate cu fiecare persoana impozabila (client/furnizor) inregistrata
                                    in scopuri de
                                    TVA sa fie consultate de catre aceasta prin intermediul aplicatiei informatice pusa
                                    la dispozitie de ANAF
                                </Text>
                                <RadioButton fx:id="schimbOptiuneDA" text="Da"/>
                                <RadioButton fx:id="schimbOptiuneNU" text="Nu"/>
                            </HBox>
                        </VBox>
                    </Tab>
                </TabPane>
            </Tab>
        </TabPane>
    </center>

    <bottom>
        <HBox prefHeight="0.0" prefWidth="897.0">
            <Button onAction="#nextTab" text="Pagina urmatoare"/>
            <Button onAction="#validate" text="Valideaza XML-ul"/>
            <Button onAction="#validateCreate" text="Valideaza XML-ul si creeaza PDF"/>
            <Button onAction="#validateCreateSign" text="Valideaza XML-ul si creeaza PDF semnat"/>
        </HBox>
    </bottom>
</BorderPane>

This is how it ends up looking setPrefWidth of the text does nothing

Making the HBoxes smaller does make the text wrap, but it ends up on top of text from the other HBoxes.

What I want is for my text to wrap to the width of the HBoxes which should be at the same size as the parent actual size.

My controller doesn't do anything in terms of styling and layout, and my .css doesn't do anything.

I'm using java8.

Upvotes: 8

Views: 18924

Answers (2)

Jim Broiles
Jim Broiles

Reputation: 413

I have had this similar need in a simple dialog box. The root of the dialog box is a 2x2 GridPane. Each row contains an HBox that spans both columns. I use a Label to display a message in the HBox in row 0. To wrap the text I use the following: Label label = new Label(message); label.setWrapText(true);

Then, of course, you add the label to the HBox. For your example, perhaps all you need to do is change Text to Label and this will work.

If you are using SceneBuilder you can go into the properties tab of the Label and click the WrapText checkbox.

Upvotes: 6

DVarga
DVarga

Reputation: 21799

You can use the wrappingWidthProperty to define the wrapping width in pixels.

You can use it in a binding:

textID.wrappingWidthProperty().bind(tabPane.widthProperty());

This binds the mentioned property to the width of the TabPane. Note: Binding to the HBox or the VBox will not work, as their width is the actual width of the Text (the HBox is resized to the width of the Text and the VBox is resized to the width of the HBox).

Upvotes: 8

Related Questions