Search code examples
javawidthtooltipjavafxword-wrap

Tooltip does not wrap/ignores max width


I am trying to add a tooltip to my ToggleButton to explain to the user what's going to happen if they press it. The problem is, my explanation is too long and the tooltip cuts it off with "..." (the ellipsis string, was it?) Anyway, when I set the max/pref width via a TooltipBuilder like in this example:

this.watched = new ToggleButton("Watched?");
Tooltip tooltip = TooltipBuilder.create().wrapText(true).text(
                "Rather than specifying individual images, you can make this source \'watched\',which means that every" + 
                " time this gallery is viewed, all the images in this directory will be listed.").build();
tooltip.prefWidth(50);
watched.setTooltip(tooltip);

I get this result:

tooltip

My attempts to set the width via maxWidth(double), maxWidthProperty().set(double), prefWidth(double), and prefWidthProperty().set(double) have been ignored.

Is there any way to overcome this?


Solution

  • In my code, I was setting wrapText(boolean) before setting the width. It seems like JavaFX doesn't like this so much. I changed it to look something like this:

    TooltipBuilder.create().prefWidth(300).wrapText(true).<other properties>.build();
    

    This gives me a successful wrap!

    enter image description here