Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 263
Radu Stefan Popescu Avatar asked Nov 16 '16 12:11

Radu Stefan Popescu


People also ask

How to make label text wrap JavaFX?

To avoid this you can wrap the text within the specified width you need to invoke the setWrapText() method. On passing true as an argument to this method the contents of the label exceeding the specified width will be wrapped into the next line.

Can you display multiple lines of text in a label JavaFX?

Can you display multiple lines of text in a label JavaFX? You can display multi-line read-only text in a Label . If the text has \n (newline) characters in it, then the label will wrap to a new line wherever the newline character is.

How do I change the text in a JavaFX label?

You can change the text of a label using its setText() method. This can be done while the application is running. Here is an example of setting the text of a JavaFX Label: label.

What is TextFlow JavaFX?

TextFlow is special layout designed to lay out rich text. It can be used to layout several Text nodes in a single text flow.


2 Answers

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).

like image 96
DVarga Avatar answered Sep 18 '22 13:09

DVarga


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.

like image 25
Jim Broiles Avatar answered Sep 20 '22 13:09

Jim Broiles