If you wrap the tooltip in <html> and </html> tags, you can break lines with <br> tags. See https://web.archive.org/web/20060625031340/http://www.jguru.com/faq/view.jsp?EID=10653 for examples and discussion. Main take-awy from that discussion: fButton.setToolTipText("<html><font face=\"sansserif\" color=\"green\">first line<br>second line</font></html>");
Use HTML tooltips and manually break your lines (a simple word tokenizer with a fixed line length should do it). Just make sure your tooltip text starts with "<HTML>". Break lines with "<BR/>" or "<P>". I realize it's not the most clean solution and Java's HTML support is horrible, but it should get things done.
This could be improved somewhat, but my approach was a helper function called before setting tooltip that split the tooltip text at provided length, but adjusted to break words on space where possible.
If you just add <html> to your tooltip text, it will appear to work until you have /*...*/ or HTML in your text. Use <html><pre> or escape your text. I also had to use <font size=3> to get it looking somewhat decent.
I created a utility class that automatically formats strings to a specific length with <br> tags. It is based on the MultiLineToolTips class posted by Paul Taylor, but his has a bug in it that skips portions of the string and does not actually limit the string to a specific length.
To use my class, simply invoke the splitToolTip method by writing MultiLineToolTips.splitToolTip(yourString); or MultiLineToolTips.splitToolTip(yourString, maxLength); if you want to split it to a specific maximum length. This will create nicely formatted tool tip strings.
import java.util.ArrayList;
import java.util.List;
/** A helper class to split strings into a certain length,
* formatted with html {@literal<br>} tags for multi-line tool tips.
* Based on the MultiLineToolTips class posted by
* <a href="https://stackoverflow.com/users/1480018/paul-taylor">Paul Taylor</a>
* on <a href="https://stackoverflow.com/a/13503677/9567822">Stack Overflow</a>
* @author <a href="https://stackoverflow.com/users/9567822/andrew-lemaitre?tab=profile">Andrew LeMaitre</a>
*/
public final class MultiLineToolTips {
/** Private constructor for utility class. */
private MultiLineToolTips() {
}
/** Default max length of the tool tip when split with {@link #splitToolTip(String)}. */
private static final int DIALOG_TOOLTIP_MAX_SIZE = 75;
/** A function that splits a string into sections of {@value #DIALOG_TOOLTIP_MAX_SIZE} characters or less.
* If you want the lines to be shorter or longer call {@link #splitToolTip(String, int)}.
* @param toolTip The tool tip string to be split
* @return the tool tip string with HTML formatting to break it into sections of the correct length
*/
public static String splitToolTip(final String toolTip) {
return splitToolTip(toolTip, DIALOG_TOOLTIP_MAX_SIZE);
}
/** An overloaded function that splits a tool tip string into sections of a specified length.
* @param toolTip The tool tip string to be split
* @param desiredLength The maximum length of the tool tip per line
* @return The tool tip string with HTML formatting to break it into sections of the correct length
*/
public static String splitToolTip(final String toolTip, final int desiredLength) {
if (toolTip.length() <= desiredLength) {
return toolTip;
}
List<String> parts = new ArrayList<>();
int stringPosition = 0;
while (stringPosition < toolTip.length()) {
if (stringPosition + desiredLength < toolTip.length()) {
String tipSubstring = toolTip.substring(stringPosition, stringPosition + desiredLength);
int lastSpace = tipSubstring.lastIndexOf(' ');
if (lastSpace == -1 || lastSpace == 0) {
parts.add(toolTip.substring(stringPosition, stringPosition + desiredLength));
stringPosition += desiredLength;
} else {
parts.add(toolTip.substring(stringPosition, stringPosition + lastSpace));
stringPosition += lastSpace;
}
} else {
parts.add(toolTip.substring(stringPosition));
break;
}
}
StringBuilder sb = new StringBuilder("<html>");
for (int i = 0; i < parts.size() - 1; i++) {
sb.append(parts.get(i) + "<br>");
}
sb.append(parts.get(parts.size() - 1));
sb.append(("</html>"));
return sb.toString();
}
}