Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I wrap text in a Tooltip with javafx?

I have an ArrayList that I am trying to get into a Tooltip when the mouse hovers over a Node. I have everything set, but I can't figure out how to wrap text in the Tooltip in order to display each item in the ArrayList on a separate line in the Tooltip.

like image 511
dgelinas21 Avatar asked Feb 22 '17 05:02

dgelinas21


People also ask

How do you wrap text in Javafx?

As a solution you can wrap the text within the width of the window by setting the value to the property wrapping with, using the setWrappingWidth() method. This method accepts a double value representing the width (in pixels) of the text.

How to use Tooltip in javafx?

You use the following approach to set a Tooltip on any node: Rectangle rect = new Rectangle(0, 0, 100, 100); Tooltip t = new Tooltip("A Square"); Tooltip. install(rect, t); This tooltip will then participate with the typical tooltip semantics (i.e. appearing on hover, etc).

What is getToolTipText?

String getToolTipText() Returns the string that was previously specified with setToolTipText .


2 Answers

Solution which does not rely on the deprecated TooltipBuilder:

Tooltip tooltip = new Tooltip(str);
tooltip.setPrefWidth(100);
tooltip.setWrapText(true);
like image 56
jewelsea Avatar answered Sep 22 '22 20:09

jewelsea


You have to define 2 different properties, a prefWidth for your tooltip and set wrapTextProperty property to true.

Your code will somewhat look like this

Tooltip t = TooltipBuilder.create().text(str).prefWidth(100).wrapText(true).build();
like image 30
Raman Sahasi Avatar answered Sep 23 '22 20:09

Raman Sahasi