views:

2497

answers:

4

I'm trying to display tooltips in Java which may or may not be paragraph-length. How can I word-wrap long tooltips?

+11  A: 

If you wrap the tooltip in <html> and </html> tags, you can break lines with <br> tags. See http://www.jguru.com/faq/view.jsp?EID=10653 for examples and discussion.

Or you can use the JMultiLineToolTip class that can be found many places on the net, including http://www.codeguru.com/java/articles/122.shtml

Paul Tomblin
+1 for diligence for finding resources that I was too lazy to find
basszero
I used the JMultiLineToolTip class you suggested. Once I used setFixedWidth() to limit the tooltip to a reasonable size, it worked great. Thanks!
Amanda S
+1  A: 

Use HTML tooltips and manually break your lines (a simple word tokenizer with a fixed line length should do it). Just make sure your tooltop 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.

basszero
+1  A: 

Tooltip text which starts with "<html>" will be treated as HTML. Of course that might be very wide HTML.

You can override JComponent.createTooltip to replace the tooltip with your own component which can display whatevee you like.

Tom Hawtin - tackline
+1 for explaining how to replace the default tooltip
Amanda S
+1  A: 

You can subclass JToolTip, which is a Component, and override createToolTip() on the component.