I have tree with several nodes and also create one tree listener for that,now i need to get the specific node path when it is clicked, I have tried this code but didn't get exact output can anybody help me?
public class FTListener implements TreeSelectionListener {
@Override
public void valueChanged(TreeSelectionEvent e) {
TreePath[] tree=new TreePath[e.getNewLeadSelectionPath().getPathCount()];
int i=0;
tree=e.getPaths();
for(TreePath tr:tree){
System.out.println(tree[i]);
i++;}
//getPath() returns the array elements so i'm here using
//for loop for printing each elements.
this is the output
[/Desktop, /home/user/Desktop, /home/user/Desktop/1302677132563_USER_MANUAL_SMS_BANKING.pdf]
[/Desktop, /home/user/Desktop, /home/user/Desktop/Lab090C .java~]
Do this
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null) {
//since Nothing is selected.
return;
}
Object nodeObject = node.getUserObject();
System.out.println("Selected node : " + nodeObject);
}
Add this if you want only single selection
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);