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 JTree Explorer with Desktop, My Computer, My Documents, My Network Places etc. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree Explorer with Desktop, My Computer, My Documents, My Network Places etc." Watch "JTree Explorer with Desktop, My Computer, My Documents, My Network Places etc." New topic
Author

JTree Explorer with Desktop, My Computer, My Documents, My Network Places etc.

Florin Constantinescu
Greenhorn

Joined: Oct 26, 2009
Posts: 10
Hi.
I am building a JTree File Explorer embedded in an Applet. The explorer should display a hierarchy like this(on Windows):

- Desktop
------ My Computer
------------- C:\
------------- D:\
------------- ..............
------ My Documents
------ My Network Places
------ .............................

etc.

The problem is that I can't access the needed "virtual" paths in the hierarchy with Java methods. All I can access is something

like: THE DESKTOP PATH(as
"C:\Documents and Settings\User\Desktop"), the partitions' paths("C:\" etc) using File or FileSystemView.
What can I do to get the job done and display the tree AS ABOVE? Should I create "manually" the hierarchy above the system root

folders(I don't really like this solution)? Is there a much convenient and elegant way to do it(something like - just grab the

virtual Desktop(not the one that physically exist at "C:\Documents and Settings\User\Desktop") and and expand it using the JTree)?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

What's wrong with JFileChooser?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Florin Constantinescu
Greenhorn

Joined: Oct 26, 2009
Posts: 10
Rob Prime wrote:What's wrong with JFileChooser?


I need to display a tree there, so I don't see JFileChooser as quite appropriate for this kind of job.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10360
    
    8

Wilhelm Canaris wrote:.

Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Wilhelm Canaris wrote:
Rob Prime wrote:What's wrong with JFileChooser?


I need to display a tree there, so I don't see JFileChooser as quite appropriate for this kind of job.

Then look for the src.zip file in your JDK folder, open it and look for the source of JFileChooser. There you can see how they did it.

Although I think I already know; check out javax.swing.filechooser.FileSystemView. Use the static factory method to get an instance, then use getRoots() to get the roots, and go one from there. A little test program to show you:
Florin Constantinescu
Greenhorn

Joined: Oct 26, 2009
Posts: 10
Rob Prime wrote:
Wilhelm Canaris wrote:
Rob Prime wrote:What's wrong with JFileChooser?


I need to display a tree there, so I don't see JFileChooser as quite appropriate for this kind of job.

Then look for the src.zip file in your JDK folder, open it and look for the source of JFileChooser. There you can see how they did it.

Although I think I already know; check out javax.swing.filechooser.FileSystemView. Use the static factory method to get an instance, then use getRoots() to get the roots, and go one from there. A little test program to show you:



Thank you very much for your tips. I realized that I made a serious mistake, something like an intermediate step to sort the files. I have introduced the paths of the files obtained with view.getFiles (...) in a collection and I sorted it with Collections.sort (...). Then I recreated the files using these paths and I've formed nodes. Obviously, it's a buggy approach. The path of My Computer is displayed somewhat like this: ":: (208D2C60-3AEA-1069-A2D7-08002B30309D)"; taken from the registry keys, I presume. (N.B. I've been using a snippet where this kind of solution took shape:
http://www.java2s.com/Code/Java/Swing-JFC/DisplayafilesysteminaJTreeview.htm ,
slightly modified by me). Sorting must be done for the files returned by view.getFiles (...), using a Comparator.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTree Explorer with Desktop, My Computer, My Documents, My Network Places etc.