Is there a way to get the selected path of a selected node in a JTree like using something like
String pathForNode = JTree.getLastSelectedPathComponent().getPath().toString();
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath tp = e.getNewLeadSelectionPath();
if (tp != null) {
pathForNode = tp.getLastPathComponent();
}
}
});
http://www.coderanch.com/t/453540/GUI/java/Getting-path-file-selected-JTree
Edit:
Try
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
doMouseClicked(me);
}
});
}
void doMouseClicked(MouseEvent me) {
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
if (tp != null) {
System.out.println(tp.toString());
}
}
JTree path
I used this:
jTreeVarSelectedPath = "";
Object[] paths = jTreeDirectorios.getSelectionPath().getPath();
for (int i=0; i<paths.length; i++) {
jTreeVarSelectedPath += paths[i];
if (i+1 <paths.length ) {
jTreeVarSelectedPath += File.separator;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With