Java Reference
In-Depth Information
} else {
tipKey = tree.convertValueToText(value, selected, expanded, leaf,
row, hasFocus);
}
renderer.setToolTipText((String)tipTable.get(tipKey));
}
return renderer;
}
}
Note
The example in Listing 17-6 takes advantage of the
JTree
method
public String
convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf,
int row boolean hasFocus)
to convert the tree node value to a text string. The value parameter is
normally a
DefaultMutableTreeNode
, described later in this chapter. When the value parameter is not a
DefaultMutableTreeNode
, using
convertValueToText()
allows the renderer to support other types of
tree nodes.
■
Using the new
ToolTipTreeCellRenderer
class simply involves creating the
Properties
list,
filling it with tooltip text for the necessary nodes, and then associating the renderer with the
tree. Figure 17-11 shows the renderer in action.
Figure 17-11.
A JTree with tooltips, using the new ToolTipTreeCellRenderer
The complete sample program used to generate the screen in Figure 17-11 is shown next
in Listing 17-7. This tree uses the list of system properties as the tree nodes. The tooltip text is
the current setting for the specific property. When using the
ToolTipTreeCellRenderer
, be sure
to register the tree with the
ToolTipManager
.
Listing 17-7.
Using a Tooltip Cell Renderer
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.util.*;