关于同TreeModel实现JTree全部展开的打印问题?应该如何打印文件名呢?

haoyuheng 2009-09-07
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.io.File;
import java.util.*;
public class TTreeModel extends JFrame{
    public TTreeModel(){
        super("TTreeModel");
        FileSystemModel fileSystemDataModel=new FileSystemModel();
        JTree tree=new JTree(fileSystemDataModel);
        tree.setScrollsOnExpand(false);
        JScrollPane scrollPane=new JScrollPane(tree);
        getContentPane().add(scrollPane);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setSize(300,250);
        show();
    }
    public static void main(String args[]){
        TTreeModel frame=new TTreeModel();
    }
}
class FileSystemModel implements TreeModel{
    private String root;
    private Vector listeners;
    public FileSystemModel(){
        File fi = new File(".");
        root = fi.getPath();         
        listeners=new Vector();
    }
    public Object getRoot(){
        return(new File(root));
    }
    public Object getChild(Object parent,int index){
        File directory=(File) parent;
        String[] directoryMembers=directory.list();
        return(new File(directory,directoryMembers[index]));
    }
    public int getChildCount(Object parent){
        File fileSystemMember=(File) parent;
        if(fileSystemMember.isDirectory()){
            String[] directoryMembers=fileSystemMember.list();
            return directoryMembers.length;
        }else {return 0;}
    }
    public int getIndexOfChild(Object parent,Object child){
        File directory=(File)parent;
        File directoryMember=(File)child;
        String[] directoryMemberNames=directory.list();
        int result=-1;
        for(int i=0;i<directoryMemberNames.length;++i){
            if(directoryMember.getName().equals(directoryMemberNames[i])){
                result=i;
                break;
            }
        }
        return result;
    }
    public boolean isLeaf(Object node){
        return((File)node).isFile();
    }
    public void addTreeModelListener(TreeModelListener l){
        if(l!=null&&!listeners.contains(l)){
            listeners.addElement(l);
        }
    }
    public void removeTreeModelListener(TreeModelListener l){
        if(l!=null){
            listeners.removeElement(l);
        }
    }
    public void valueForPathChanged(TreePath path,Object newValue){}
}

就以上代码,如何改写,成为只打印文件名的树呢?
haoyuheng 2009-09-07
我对TreeModel如何实现打印的过程不清楚,所以不能只打印文件名,而打印了绝对路径
<>++< 2010-11-25
haoyuheng 写道
我对TreeModel如何实现打印的过程不清楚,所以不能只打印文件名,而打印了绝对路径
你们说的打印是什么意思啊?
可以显示文件名的 可以不显示文件的绝对路径
Global site tag (gtag.js) - Google Analytics