sandip chitale's blog    Sandip Chitale's blog (scblog)
NOTE: I have moved many of my modules to NetBeans Plugin Portal . Please check there for latest versions of modules described on this blog.
20061014 Saturday October 14, 2006

TIP: Multiline tooltip

Say you want to display the message from a Throwable in a JLabel and the Throwable's stack trace in the label's tootip - you could do something like the code below:

From the GUI standards point of view this may not be a good practice because the tooltip text is not selectable/copyable. I am using the contrived example only to demonstrate the tip. In fact I wish the Swing tooltip had a mode in which the tooltip text could be selected/copied.

        Throwable throwable = new Throwable("Sample message from a Throwable");
       
        JLabel messageLabelWithSingleLineTooltip = ...;
	StringWriter stringWriter = new StringWriter();
        throwable.printStackTrace(new PrintWriter(stringWriter)); // capture the stack trace as a string
        messageLabelWithSingleLineTooltip.setText(throwable.getMessage() + "(Single line tooltip)");
        messageLabelWithSingleLineTooltip.setToolTipText(
                stringWriter.toString()
                );

You get the following wide, single line tooltip (truncated to keep the image size small).

Not very useful. The stack trace is very hard to read. The problem happens because normally the tooltip text is shown as a single line of text where as the stack trace is in the form of a multiline text. The newlines are simply stripped off. There is a simple solution to the problem though. The Swing tooltip supports html syntax. Thus you can use "<html><pre>" + multilineText  + "</pre>" and let the <pre> tag handle the multiline text. Here is the code:

        JLabel messageLabelWithMultiLineTooltip = ...;
	messageLabelWithMultiLineTooltip.setText(throwable.getMessage() + "(Multiline tooltip)");
        messageLabelWithMultiLineTooltip.setToolTipText(
                "<html><pre>"
                + stringWriter.toString()
                + "</pre>"
                );

With the above code you get: multil line tooltip as shown below:

For the details of html support in Swing components see: Using HTML in Swing Components. A lot of interesting things can be done with this functionality.

If you want to play with code here is a Netbeans 5.5 project.

DISCLAIMER: This code is experimental. So no guarantees. Use the code at your own risk.

Also see TIP: Images in Tooltips blog entry which is based on the same technique.


Posted by sandipchitale ( Oct 14 2006, 05:42:48 PM PDT ) Permalink Comments [2]


Trackback URL: http://blogs.sun.com/scblog/entry/tip_show_mutiline_text_in
Comments:

This is very good. However in Netbeans 5.5 when trying to use this for the tool tip of a property you will get the html tags showing up in the description area. It seems Netbeans is using that short description for two purposes even though each purpose handles the string differently.

Posted by David Havrda on January 26, 2007 at 12:38 PM PST #

It’s very good article. Great site with very good look and perfect information. I like it too.

Posted by mckay on June 03, 2008 at 08:07 AM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed








« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today

Get NetBeans 5.5

Locations of visitors to this page

Today's Page Hits: 434


XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

scblog
scblog