473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

TreeModel and filesystem

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
0 1521

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sydney | last post by:
Hello, I have a problem to catch tree events using a JTree associated with a custom TreeModel. I would like an editable JTree, and when the user updates some nodes of the tree, I would like the...
9
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
1
by: Francois Fernandes | last post by:
Hi list! I would like to write a linux c++ program which mounts it's own filesystem within a file, that is located in the Linux filesystem. Now I don't know how to start that. I need basic help...
3
by: Michael | last post by:
Is this the correct place to ask about Boost libs? If it is.............. #include <boost/filesystem/path.hpp> #include <boost/filesystem/operations.hpp> using namespace boost; using...
1
by: Lucas Fletcher | last post by:
Hi All, I've searched the web for an article explaining the many tradeoffs between storing your XML in a database vs the filesystem but I haven't really found anything of note. This is the...
2
by: Jeff Davis | last post by:
I have virtual hosted users on a server and some of them have a postgresql database. I'm concerned about the outside possibility that a user could create an infinite loop and fill up the partition...
16
by: Michal Hlavac | last post by:
Hello, I am working on web portal. There are some ads. We have about 200 000 ads. Every ad have own directory called ID, where is 5 subdirectories with various sizes of 5 images. Filesystem...
5
by: GaryE | last post by:
Hello: I am having trouble linking a couple of files using the boost::filesystem. I am using MSVC 6.0. Here is an abbreviated version of my problem: foo.h: #ifndef __FOO_ #define...
1
by: Thomas W | last post by:
I want to create a virtual filesystem based on a relational database. It might run as a server on a different machine, but it has to be available like a normal filesystem, either shared using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.