28.12.2012 Views

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1266 Chapter 10 The Tree Control<br />

Node-specific Tooltips Using a Custom Renderer<br />

A third approach to the problem, and the most powerful, is to implement<br />

tooltip support in the renderer. This makes it possible to return a different<br />

tooltip for each node if necessaiy. As we said in the previous section, JTree<br />

overrides the getToolTipText method to get the tooltip for each node<br />

from that node's renderer. It does this by calling the Tenderer's getTree-<br />

CellRendererComponent method as if it were going to render the node<br />

and then invokes the getToolTipText method of the returned component<br />

to get the tooltip for the node. If you want your nodes to display tooltips, you<br />

simply have to call set Tool TipText on the component that you return<br />

from the Tenderer's getTreeCellRendererComponent method.<br />

To demonstrate the general technique, we'll modify the custom renderer<br />

shown in Listing 10-6 to include a tooltip. For simplicity, we'll make the tooltip<br />

be the same text as would be displayed on the node itself. In a real-world<br />

application, you would probably store the tooltip as part of the node or its<br />

user object and code the renderer to extract it from there. Setting the tooltip<br />

requires only one extra line of code in the renderer's getTreeCellRendererComponent<br />

method:<br />

// By default, use the same value as the<br />

// node text for the tooltip, for leaf nodes only<br />

((JComponent)c).setToolTipText(<br />

leaf ? value.toString() : null);<br />

where the variable c is the component being returned by the renderer. In<br />

this example, we set a tooltip only for leaf nodes.<br />

Having made this change, you just need to register the tree with the Tool-<br />

TipManager, as shown earlier in this section, and your tree will display a different<br />

tooltip for each leaf node. However, as with the first approach we took<br />

to displaying tooltips, a tooltip will only be displayed when the mouse is over<br />

an area that will be drawn by a renderer. To arrange for a tooltip to be displayed<br />

wherever the mouse is, we can combine this approach with the previous<br />

one by subclassing JTree and implementing a cleverer version of<br />

getToolTipText:<br />

public String getToolTipText(MouseEvent evt) {<br />

// Let the renderer supply the tooltip<br />

String tip = super.getToolTipText(evt);<br />

}<br />

// If it did not, return the tree's tip<br />

return tip != null ? tip : getToolTipText();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!