By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,538 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,538 IT Pros & Developers. It's quick & easy.

TreeModel and filesystem

P: n/a
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;
}
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.