views:

38

answers:

0

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();

}

}

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.io.File
at action.FileTreeModel.isLeaf(Diary.java:323)
at javax.swing.plaf.basic.BasicTreeUI$Handler.valueChanged(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(Unknown Source)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(Unknown Source)
at javax.swing.JTree.setSelectionPath(Unknown Source)
at action.Diary$3.actionPerformed(Diary.java:174)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)