JTree TreePath casting problem

Posted by newbie123 on Stack Overflow See other posts from Stack Overflow or by newbie123
Published on 2010-06-15T16:17:43Z Indexed on 2010/06/15 16:22 UTC
Read the original article Hit count: 239

Filed under:
|
|
|

I got this casting problem java.lang.String cannot be cast to java.io.File, when I trying to do this

TreePath p =  new TreePath(new  Object[] {"src","file","My Diary" });

This is my jtree file model

class FileTreeModel implements TreeModel {
private FileNode root;

public FileTreeModel(String directory) {
    root = new FileNode(directory);
}

public Object getRoot() {
    return root;
}

public Object getChild(Object parent, int index) {
    FileNode parentNode = (FileNode) parent;
    return new FileNode(parentNode, parentNode.listFiles()[index].getName());
}

public int getChildCount(Object parent) {
    FileNode parentNode = (FileNode) parent;
    if (parent == null || !parentNode.isDirectory()
            || parentNode.listFiles() == null) {
        return 0;
    }

    return parentNode.listFiles().length;
}

public boolean isLeaf(Object node) {
    return (getChildCount(node) == 0);
}

public int getIndexOfChild(Object parent, Object child) {
    FileNode parentNode = (FileNode) parent;
    FileNode childNode = (FileNode) child;

    return Arrays.asList(parentNode.list()).indexOf(childNode.getName());
}

public void valueForPathChanged(TreePath path, Object newValue) {

}

public void addTreeModelListener(TreeModelListener l) {
}

public void removeTreeModelListener(TreeModelListener l) {
}

}

class FileNode extends java.io.File {

public FileNode(String directory) {
    super(directory);
}

public FileNode(FileNode parent, String child) {
    super(parent, child);
}

@Override
public String toString() {
    return getName();

}

}

© Stack Overflow or respective owner

Related posts about java

Related posts about file