Java JTree get tree path from mouse event

Description

Java JTree get tree path from mouse event

import java.awt.event.MouseEvent;

import javax.swing.JTree;
import javax.swing.tree.TreePath;

public class Main {

   public static TreePath getTreePath(MouseEvent evt) {
      if (evt == null) {
         throw new NullPointerException("evt == null");
      }/*w  w  w  .j  a  v  a 2 s.co m*/
      Object source = evt.getSource();
      if (source instanceof JTree) {
         int mousePosX = evt.getX();
         int mousePosY = evt.getY();
         return ((JTree) source).getPathForLocation(mousePosX, mousePosY);
      }
      return null;
   }
}



PreviousNext

Related