2

I have a JTree and I want to show some description to the user when he hovers his mouse over the tree nodes.

I read from documentation that we can use the MouseMotionListener for this. But how can I get the value of the node over which the mouse was moved?

Any pointers will be very useful.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
bharz629
  • 161
  • 2
  • 4
  • 10

2 Answers2

2

One way to get the value of the node from the MouseEvent is to get a TreePath for the x,y location of the mouse and fetch the last path component from this path.

This could look something like this:

import javax.swing.tree.TreePath;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JTree;
...

final JTree tree = new ...
tree.addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseMoved(MouseEvent e)
        {
            TreePath path = tree.getPathForLocation(e.getX(), e.getY());
            if(path == null || path.getLastPathComponent() == null)
            {
                return;
            }

            Object nodeHoveredOver = path.getLastPathComponent();
            System.out.println(path.getLastPathComponent());
        }

        @Override
        public void mouseDragged(MouseEvent e)
        {
            ...                
        }
});

However, if you want to display your description as a tooltip then this this solution might work better for you.

Community
  • 1
  • 1
cyon
  • 9,340
  • 4
  • 22
  • 26
  • I want to display a tooltip for each node.For that I need to identify what is the node which was hovered over and how to display the tooltip.Do you have any suggestions? – bharz629 Oct 22 '12 at 11:49
  • The function `path.getLastPathComponent()` will return the node which was hovered over. The `TreeCellRenderer` of the `JTree` provides tooltips for the nodes. Follow the link at the end of the post to see how to use the `TreeCellRenderer` to specify the tooltip. – cyon Oct 22 '12 at 12:06
1

Take a look at the JTree#getTooltipText implementation:

public String getToolTipText(MouseEvent event) {
    String tip = null;

    if(event != null) {
        Point p = event.getPoint();
        int selRow = getRowForLocation(p.x, p.y);
        TreeCellRenderer       r = getCellRenderer();

        if(selRow != -1 && r != null) {
            TreePath     path = getPathForRow(selRow);
            Object       lastPath = path.getLastPathComponent();
            Component    rComponent = r.getTreeCellRendererComponent
                (this, lastPath, isRowSelected(selRow),
                 isExpanded(selRow), getModel().isLeaf(lastPath), selRow,
                 true);

            if(rComponent instanceof JComponent) {
                //...

                tip = ((JComponent)rComponent).getToolTipText(newEvent);
            }
        }
    }
    // No tip from the renderer get our own tip
    if (tip == null) {
        tip = getToolTipText();
}
    return tip;
}

So the tooltip text as specified by the renderer is respected.

Robin
  • 36,233
  • 5
  • 47
  • 99