7

I want to add multiline tooltip message to my swing component. The basic solution is to use html tags like this:

label.setToolTipText("<html><p width=\"350px\">" + text + "</p></html>");

It works fine with long text. But if the text, let's say, contains only one word it also will have fixed 350px width with a lot of empty space.


Is there any special html property for max-width which will work properly with setToolTipText() method? I tried the next solution but it doesn't work:

"<p style=\"max-width: 350px\">"

Should I calculate text width in pixels and change opening <p> tag if width lesser than 350 px?

ferrerverck
  • 618
  • 3
  • 9
  • 17

5 Answers5

1

Following code will dynamically set the size of the tooltip.

If it is a single word, it will set the size according to the pixel size of the text. (There will be no empty space). If it is a large paragraph, it will expand up to 350 pixel max, and display the text in multiple lines.

FontMetrics fontMetrics = label.getFontMetrics(label.getFont());
int length = fontMetrics.stringWidth(text);
label.setToolTipText("<html><p width=\"" + (length > 350 ? 350 : length) + "px\">" + text + "</p></html>\"");

Screenshot - Tooltip: Single word

Screenshot - Tooltip: Multiple words

Screenshot - Tooltip: Paragraph

Arindam Roy
  • 111
  • 2
  • thank you for your answer, Arindam! answers with code and no explanation are generally not considered ideal for the purposes of stack overflow. could you please edit in an english description of how you think this addresses the question, and what it does? – Sasha Kondrashov Jul 29 '20 at 08:25
  • Thanks @SashaKondrashov for your suggestion. I have added my explanation. – Arindam Roy Jul 29 '20 at 17:48
  • This one is almost right, the problem is that px in swing does not map directly to swing lengths. You want "pt" rather then "px" . See the following for more details: https://stackoverflow.com/questions/56380978/java-html-rendering-pt-vs-px-sizes – Emily Crutcher Oct 16 '20 at 13:02
0

Your should to use the <br/> tag to down one line, like this:

JButton button = new JButton("click me");
button.setToolTipText("<html>multy-line<br/>tool-tip</html>");
Programmer
  • 803
  • 1
  • 6
  • 13
0

I like the idea of wrapping the tooltip text with <html><p width 350px>text</html>, but some of the suggested solutions have various problems, the most common are: 1) always using an arbitrary width, 2) using the length of the entire text, and 3) using the wrong font to compute the desired width. I offer the following that addresses those issues.

public final class HtmlTooltip {

private static int         sWrapLength  = 350;
private static FontMetrics sFontMetrics = new JLabel().getFontMetrics(UIManager.getFont("ToolTip.font"));

/**
 * Add HTML tags to force it to wrap at a defined length. <pre>{@code
 *  component.setToolTipText(toString(text));
 * }</pre>
 *
 * @param html
 *            String
 * @return update string ready to be used with setToolTipText
 * @see JComponent#setToolTipText(String)
 */
public static String toString(final String html) {
    final int length = sFontMetrics.stringWidth(longest(html));
    return String.format("<html><p width=%d\"pt\">%s</html>",
                         length > sWrapLength ? sWrapLength : length,
                         html);
}

/**
 * Given a complete HTML string, find the longest piece between tags.
 *
 * @param html
 *            String
 * @return longest piece
 */
private static String longest(final String html) {
    return Arrays.asList(html.split("<"))
                 .stream()
                 .max(Comparator.comparingInt(String::length))
                 .get();
}

private HtmlTooltip() {
}

}

B. Stackhouse
  • 477
  • 5
  • 15
0

Define a custom BasicToolTipUI with overrides getPreferredSize. If text contains "", apply wanted width. HTML rendering will follow.

Werehog83
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 06 '21 at 15:49
-2

I would suggest the following:

After setting a fixed width, you could get the preferred size using:

Dimension d = someToolTip.getPreferredSize();
if (d.width < 350) {

    // Insert code to make tooltip-size smaller, by setting it to d.width;

}
Luke
  • 409
  • 3
  • 12
  • Hi Luke, your idea will make a sense if you attach at least partial solution. E.g. preferred size in my case return the same value as fixed size though it can be made much less. Either you mean something else or you just suggested idea without checking it first. – nickolay.laptev Aug 18 '14 at 07:35
  • Set the tooltip the way you desire it to be, get the current size via getPreferredSize() and then set that size in case you wish it to be smaller. So manually alter the width instead of setting a maximum. – Luke Aug 29 '14 at 00:54