How to get selected node from JTree after selecting it?

GemHunter1 picture GemHunter1 · Jan 17, 2013 · Viewed 30.4k times · Source

Possible Duplicate:
JTree: how to get the text of selected item?

1In C# there's TreeView_afterSelect event, and i ask if in java is something like that ? I used : hierarchyPropertyChange but it runs two times...

Answer

Reimeus picture Reimeus · Jan 17, 2013

You can use a TreeSelectionListener:

tree.addTreeSelectionListener(new TreeSelectionListener() {

@Override
public void valueChanged(TreeSelectionEvent e) {
   DefaultMutableTreeNode selectedNode = 
       (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); 
   ...          
  }
});