Radu Stefan Popescu Radu Stefan Popescu - 22 days ago 4
Java Question

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
HBox
es smaller does make the text wrap, but it ends up on top of text from the other
HBox
es.

What I want is for my text to wrap to the width of the
HBox
es 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.

Answer

You can use the wrappingWidthProperty to define the width in pixels where the text should be wrapped.

You can use for example a binding for this:

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 Text and the VBox is resized to the HBox).