Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2004

Tip: Looking for answers? Try searching our database.

TreeModel and filesystem

Thread view: 
fishfry - 24 Jun 2004 07:54 GMT
I'm using a TreeModel to represent a filesystem directory tree for
presentation by a JTree. The first level of the directory appears fine,
but when I click on any of the little triangles to expand a
subdirectory, I get an exception,

apple.awt.EventQueueExceptionHandler Caught Throwable :
java.lang.NullPointerException
       at FileSystemTreeModel.getChild(Unknown Source)

This is on Mac OS X 10.3, JDK 1.4.2.

Here are some of the methods I implemented for my TreeModel. I am
thinking that perhaps I need to do something special when the user
clicks on the triangle? I don't quite understand that ... why wouldn't
the JTree just call back to the appropriate methods of my TreeModel?

Any advice appreciated.

class FileSystemTreeModel implements TreeModel {
   String dir;
       
   public FileSystemTreeModel (String dir) {
       this.dir = dir;
   }

   public Object getRoot() {
       return new File(dir);
   }

   public Object getChild(Object parent, int index) {
       File f = (File)parent;
       if (! f.isDirectory()) {
           return null;
       }
       
       MyFile mf = new MyFile(parent.toString());
       File[] files = mf.listFiles();
       return files[index];
   }
       
   public int getChildCount(Object parent) {    
       File f = (File)parent;      
       
       if (! f.isDirectory()) {
           return 0;
       }
       
       MyFile mf = new MyFile(f.getAbsolutePath());
       File[] files = mf.listFiles();
       return files.length;    
   }
       
   public boolean isLeaf(Object node) {
       File f = (File)node;
       return f.isFile();
   }

       
   public int getIndexOfChild (Object parent, Object child) {
       File f = (File)parent;
     
       if (! f.isDirectory()) {
           return -1;
       }
       
       MyFile mf = new MyFile(f.getAbsolutePath());
       File[] files = mf.listFiles();
       for (int i = 0; i < files.length; ++i) {
           if (files[i].equals(child)) {
               return i;
           }
       }    
       return -1;
   }
Jim Sculley - 24 Jun 2004 23:25 GMT
> I'm using a TreeModel to represent a filesystem directory tree for
> presentation by a JTree. The first level of the directory appears fine,
[quoted text clipped - 4 lines]
> java.lang.NullPointerException
>         at FileSystemTreeModel.getChild(Unknown Source)

Your model has a problem.

> This is on Mac OS X 10.3, JDK 1.4.2.
>
[quoted text clipped - 15 lines]
>         return new File(dir);
>     }

Every call to getRoot() will create a new (unnecessary) File object.

>     public Object getChild(Object parent, int index) {
>         File f = (File)parent;
>         if (! f.isDirectory()) {
>             return null;
>         }

This code can throw a NullPointerException if 'parent' is null.  The
call to isDirectory() will fail with the NPE.

<lots of other code with bad stuff snipped>

Have a look at the FileSystemView class.  You are trying to reinvent the
wheel.

Jim S.
Signature

Remove my extraneous mandibular appendages to reply via email.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.