JavaFX Button with multiple text lines
Solution 1
I resolved this problem including a VBox inside my button, and then including several Labels inside the VBox. Like this:
The result is:
If there is a more elegant way to have the same result, please, let me know. Thank you.
Solution 2
Also you can use the wrapTextProperty
. But you have to set toolbar height greater than expected button height.
Button btn = new Button();
btn.wrapTextProperty().setValue(true);
// or btn.setWrapText(true);
btn.setText("Some looooooooooooong text");
Or if you want to determine exactly where the line should be wrapped, you can go this way:
Button btn = new Button();
btn.setText("Line1\n Line2\n Line3");
Last way will work without changing toolbar height.
Solution 3
In the button text property select "switch to multi-line mode
Solution 4
From sobolev's response, you can do:
Button btn = new Button();
btn.setText("Line1\n Line2\n Line3");
button.textAlignmentProperty().set(TextAlignment.CENTER);
This will create 3 lines of text and allign them in the center of your button.
henriqueor
Senior Full Stack Developer. Addicted to JavaFX, Java, Angular, Flutter and many others... #SOreadytohelp
Updated on June 13, 2022Comments
-
henriqueor almost 2 years
I need to create a toolbar in my screen that will have multiple buttons, and each button must have multiple lines of Text. For example:
I looked over the internet and StackOverflow but I couldn't find anything showing how to do this in JavaFX. I'm using JavaFX 8.
Someone could help me, please?
Tks
-
Zeta.Investigator over 6 yearsnice suggestion :) tnx
-
Pintang over 4 yearsor if you generate you button dynamically, in code button.setWrapText(true);
-
James Robinson over 3 yearsWhen I did this the toString method of the VBox was called.