I need to display a multiline, read-only text - which control can be used for that? It should only dislay the text, like a Label does, however Label does not support multiline?
Thanks for any hint :-)
I need to display a multiline, read-only text - which control can be used for that? It should only dislay the text, like a Label does, however Label does not support multiline?
Thanks for any hint :-)
Text class also available. A good explanation in this answer label-and-text-differences-in-javafx Basically use Text for where you're not getting input, otherwise Label is better.
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.If the label has
wrapText
set to true and there is not enough space to display a single line of text, then the label will wrap the text to a new line.If you want text in different styles, then, if using JavaFX 2, place multiple labels in a
FlowPane
or, if you are using Java 8+, place the labels in aTextFlow
component.Produced by the following code:
Try running the above program and resizing the window to see the effect of the
\n
new line values in the label's text as well as thewrapText
property on the label. Vary thewrapText
setting from true to false to see the difference between havingwrapText
switched on and off.If you set a max width you want for your
Label
, then you setsetWrapText
totrue
so it displays the text multiline:You can also use
Text
to appear in multiline, by settingwrappingWidthProperty
according to your needs.In this code, I have set max width to
345.0
,So When the text's size reaches beyond345
pixels will be wrapped to next Line.It not works when you set text from FXML file as well as from visual FXML editor.