Menu

#6 Don't use Tooltips

open
nobody
5
2008-12-24
2008-12-24
Anonymous
No

While I appreciate what is trying to be done with tooltips to show the details of the active node, messing with ToolTipManager changes global settings and that means it does not play nicely with what the app may want. There is another way that won't interfere with the tooltip manager settings, but still uses the IToolTipBuilder interface.

Just paint the tooltip onto the map, like a cell renderer. This is based on an inner class that I used, so it could be cleaned up a bit if moved internal to JTreeMap. (appologies if the formatting is bad)

this.treeMap = new JTreeMap(builder.getRoot())
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
TreeMapNode node = getActiveLeaf();
if(node != null)
{
JToolTip tt = createToolTip();
int x = node.getX();
int y = node.getY();
Point pt = getToolTipLocation(new MouseEvent(this, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, x, y, 0, false, MouseEvent.BUTTON1));
g.translate(pt.x, pt.y);
tt.paint(g);
g.translate(-pt.x, -pt.y);
}
}
};
// this is needed to disable normal tooltips
this.treeMap.setToolTipText(null);

And in a mouseExited listener, I used:
this.treeMap.setActiveLeaf(null);
this.treeMap.repaint();

Discussion


Log in to post a comment.