0

I have created a scene in javafx which contains three buttons. I have add in those buttons my label. the size of my label is quite small. My problem is that the text of the label is quite big and it does not fit in one line. How can I enable to expand the text in a second line inside my button? My interface currently looks like:

enter image description here

konstantin
  • 853
  • 4
  • 16
  • 50

3 Answers3

5

You can either place \n in text in place where you want to brake the line or call setWrapText(true) on your buttons.

Stugal
  • 850
  • 1
  • 8
  • 24
  • Thanks both of them working. Can I detect somehow if the text is outside of the box? in order to change the font size correspondigly? – konstantin Sep 06 '16 at 13:46
  • 1
    have a look here: http://stackoverflow.com/questions/13015698/how-to-calculate-the-pixel-width-of-a-string-in-javafx, that should help you achieve what you want, although you might end up with too small font or have inconsistent GUI (some buttons with too big fonts, some with too small). Btw. Please accept the answer if its working :). – Stugal Sep 06 '16 at 13:48
1

1- By coding

Button btn = new Button ("can wrap that text") ;
btn.wrapTextProperty().setValue(true)

2- By FXML scene builder under the properties of button check the wrap text

<Button mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0"  text="Can wrap that text" wrapText="true"/>
Mazen Embaby
  • 1,255
  • 11
  • 18
0

use

btn.wrapTextProperty().setValue(true);

or if you have specified position to break use \n

Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
Ajay Sreeram
  • 171
  • 2
  • 18