3

basically I am using dom4j library to render the DefaultTreeModel into JTree. DefaultTreeModel parses XML document. Each XML node contains information like attributes, name, id etc.

Basically, I add a actionlistener to this Jtree. I would like to access the underlying DefaultTreeModel node containing the node's information like attributes, name etc.

 jtree.addMouseListener(new MouseInputAdapter(){
                public void mouseClicked(final java.awt.event.MouseEvent evt) {
                    int rowLocation = tree.getRowForLocation(evt.getX(), evt.getY());                           
                            if (evt.getClickCount() == 1){
                                //get this element double clicked
                                Component dblClickedElement = tree.findComponentAt(evt.getX(), evt.getY());                     
                                                    }
                    });                
                }
            });
KJW
  • 15,035
  • 47
  • 137
  • 243

1 Answers1

3

What about this?

tree.getPathForRow(rowLocation).getLastPathComponent()
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • And even easier without the need of of the intermediate row: `tree.getPathForLocation(evt.getX(), evt.getY());`. As the user might not click on an element, you might want to check the result before retrieving the `getLastPathComponent()`. – walderich Oct 07 '15 at 07:51