JavaFX Button with multiple text lines

12,557

Solution 1

I resolved this problem including a VBox inside my button, and then including several Labels inside the VBox. Like this:

enter image description here

The result is:

enter image description here

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

enter image description here"

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.

Share:
12,557
henriqueor
Author by

henriqueor

Senior Full Stack Developer. Addicted to JavaFX, Java, Angular, Flutter and many others... #SOreadytohelp

Updated on June 13, 2022

Comments

  • henriqueor
    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:

    Button with multiple lines

    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
    Zeta.Investigator over 6 years
    nice suggestion :) tnx
  • Pintang
    Pintang over 4 years
    or if you generate you button dynamically, in code button.setWrapText(true);
  • James Robinson
    James Robinson over 3 years
    When I did this the toString method of the VBox was called.