File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Getting the path of file selected in JTree item Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting the path of file selected in JTree item" Watch "Getting the path of file selected in JTree item" New topic
Author

Getting the path of file selected in JTree item

Bill Cappoli
Greenhorn

Joined: Oct 27, 2007
Posts: 3
I'm working on creating a backup application. As part of it I'm displaying the file directory of the computer in a JTree. I have been able to get everything to appear in the JTree as t is supposed to. However, no matter what I try I cannot seem to get the path of the selected file. I want to use the path to than list the selected files in the GUI as pending backup. Below I have included the code for the class as it is now where it displays the file structure. Under that is the most recent attempt to read the path. Any ideas?

Thanks



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

The model shouldn't care about its selection - that's part of the JTree. And that has a method called getSelectionPath() (or getSelectionPaths() for multiple selection).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bill Cappoli
Greenhorn

Joined: Oct 27, 2007
Posts: 3
Rob Prime wrote:The model shouldn't care about its selection - that's part of the JTree. And that has a method called getSelectionPath() (or getSelectionPaths() for multiple selection).


Thanks for the tip. I ended up moving the selection listener outside of the TreeModel class and was able to grab it using this code in case anyone else has a similar problem:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the path of file selected in JTree item