http://java.sun.com/products/jfc/tsc/articles/treetable1/index.html

解决方案 »

  1.   

    JTree, javax.swing.JTree
    JTree是java SWING中最复杂的组件之一,它最平常的应用大概是以文件夹和树页图来显示
    分层的数据,比如做个类似WINDOWS资源管理器的东东
    帖篇我写的一个有关树的类,只是个类,需要加JFRAME之类的东西哦,这个类就有点象资源管理器,界面图标用的是LINUX的KDE
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    import javax.swing.tree.*;
    import javax.swing.border.*;
    import java.applet.*;
    public class TreeDemoX extends JPanel
    {
    private JSplitPane split=null;
    private JPanel northpanel=null;
    private JPanel southpanel=null;
    private JTree tree=null;
    private JScrollPane treescroll=null;
    JScrollPane viewscroll=null;
    AudioClip treechange,treeexpand;
    private ViewPanel viewpanel=null;
    public TreeDemoX()
    {
    setLayout(new BorderLayout());
    treechange=Applet.newAudioClip(getClass().getResource("sound\\opendir.wav"));
    treeexpand=Applet.newAudioClip(getClass().getResource("sound\\treeexpand.wav"));
    treeInit();
    viewpanel=new ViewPanel();
    viewpanel.viewload(new File("c:\\"),tree.getPathForRow(0),tree);
    treescroll=new JScrollPane(tree);
    viewscroll=new JScrollPane(viewpanel);
    viewpanel.sss=viewscroll;
    splitInit();
    add(split,BorderLayout.CENTER);
    }
    public void treeInit()
    {
    tree=new JTree(createTreeModel());
    tree.putClientProperty("JTree.lineStyle","Angled");
    tree.setCellRenderer(new TreeRenderer());
    tree.addTreeExpansionListener(
    new TreeExpansionListener()
    {
    public void treeCollapsed(TreeExpansionEvent e)
    {
    treeexpand.play();
    }
    public void treeExpanded(TreeExpansionEvent e)
    {
    TreePath path=e.getPath();
    treeNode node=(treeNode)path.getLastPathComponent();
    if(!node.isex())
    {
    DefaultTreeModel model=(DefaultTreeModel)tree.getModel();
    node.explore();
    model.nodeStructureChanged(node);
    }
    treeexpand.play();
    }
    });
    tree.addTreeSelectionListener(
    new TreeSelectionListener()
    {
    public void valueChanged(TreeSelectionEvent e)
    {
    TreePath path=e.getPath();
    treeNode node=(treeNode)path.getLastPathComponent();
    viewpanel.viewload((File)(node.getUserObject()),new TreePath(node.getPath()),tree);
    treechange.play();
    viewscroll.validate();
    }
    });
    }
    public DefaultTreeModel createTreeModel()
    {
    File root=new File("C:\\");
    treeNode rootNode=new treeNode(root);
    rootNode.explore();
    return new DefaultTreeModel(rootNode);
    }
    public void splitInit()
    {
    if(treescroll!=null && viewpanel!=null)
    {
    split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,treescroll,viewscroll);
    split.setContinuousLayout(true);
    split.setOneTouchExpandable(true);
    split.setDividerLocation(200);
    }
    }
    //////////////////////////////////////////////TreeRenderer
    class  TreeRenderer extends DefaultTreeCellRenderer
    {
    ImageIcon open,close,root;
    public TreeRenderer()
    {
    open=new ImageIcon(getClass().getResource("image\\tree\\new\\diropen.gif"));
    close=new ImageIcon(getClass().getResource("image\\tree\\new\\dirclose.gif"));
    root=new ImageIcon(getClass().getResource("image\\tree\\new\\root.gif"));
    }
    public Component getTreeCellRendererComponent(JTree tree,
    Object vue,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus)
    {
    JLabel label=(JLabel)(super.getTreeCellRendererComponent(tree,vue,selected,expanded,leaf,row,hasFocus));
    if(row==0)
    setIcon(root);
    if(!leaf && row!=0)
    {
    if(hasFocus)
    setIcon(open);
    else
    setIcon(close);
    }
    return label;
    }
    }//////////////////////////////////////////////////TreeRenderer
    }
    //////////////////////
    //      treeModel       //
    /////////////////////
    class treeNode extends DefaultMutableTreeNode
    {
    boolean ex=false;
    public treeNode(File f)
    {
    setUserObject(f);
    }
    public boolean isDirectory()
    {
    return ((File)getUserObject()).isDirectory();
    }
    public boolean isex()
    {
    return ex;
    }
    public File getFile()
    {
    return (File)getUserObject();
    }
    public boolean getAllowsChildren()
    {
    return isDirectory();
    }
    public boolean isLeaf()
    {
    return !isDirectory();
    }
    public String toString()
    {
    File file=getFile();
    String filename=file.toString();
    int index=filename.lastIndexOf("\\");
    if(index!=-1 && index!=filename.length()-1)
    return filename.substring(index+1);
    else 
    return filename;
    }
    public void explore()
    {
    if(!isex() && isDirectory())
    {
    File[] files=getFile().listFiles();
    for(int i=0;i<files.length;i++)
    {
    if(files[i].isDirectory())
    add(new treeNode(files[i]));
    }
    ex=true;
    }
    }
    }
    //***************************TreeNode/////////////////////////////////
    //             ViewPanel                      //
    ////////////////////////////////
    class ViewPanel extends JPanel
    {
    ImageIcon icof,icod;
    File[] f,d;
    JScrollPane sss;
    AudioClip enterfile,opendir;
    ImageIcon zip,def,dir,doc,exe,fon,gif,htm,jpg,bmp,log,eml,music,pdf,scr,txt,vedio,wav,
    bat,dll,dat,javaf,sys,classf,asf,inf,png;
    public ViewPanel()
    {
    //setLayout(new FlowLayout());
    setBackground(Color.white);
            icof=new ImageIcon(getClass().getResource("image\\f.gif"));
    icod=new ImageIcon(getClass().getResource("image\\tree\\new\\dirclose.gif"));
    enterfile=Applet.newAudioClip(getClass().getResource("sound\\enterfile.wav"));
    opendir=Applet.newAudioClip(getClass().getResource("sound\\opendir.wav"));
    zip=new ImageIcon(getClass().getResource("image\\tree\\new\\zip.gif"));
    def=new ImageIcon(getClass().getResource("image\\tree\\new\\default.gif"));

    doc=new ImageIcon(getClass().getResource("image\\tree\\new\\doc.gif"));
    exe=new ImageIcon(getClass().getResource("image\\tree\\new\\exe.gif"));
    fon=new ImageIcon(getClass().getResource("image\\tree\\new\\fon.gif"));
    gif=new ImageIcon(getClass().getResource("image\\tree\\new\\gif.gif"));
    htm=new ImageIcon(getClass().getResource("image\\tree\\new\\htm.gif"));
    jpg=new ImageIcon(getClass().getResource("image\\tree\\new\\jpg.gif"));
    bmp=new ImageIcon(getClass().getResource("image\\tree\\new\\bmp.gif"));
    log=new ImageIcon(getClass().getResource("image\\tree\\new\\log.gif"));
    eml=new ImageIcon(getClass().getResource("image\\tree\\new\\eml.gif"));
    music=new ImageIcon(getClass().getResource("image\\tree\\new\\music.gif"));
    pdf=new ImageIcon(getClass().getResource("image\\tree\\new\\pdf.gif"));
    scr=new ImageIcon(getClass().getResource("image\\tree\\new\\scr.gif"));
    txt=new ImageIcon(getClass().getResource("image\\tree\\new\\txt.gif"));
    vedio=new ImageIcon(getClass().getResource("image\\tree\\new\\video.gif"));
    wav=new ImageIcon(getClass().getResource("image\\tree\\new\\wav.gif"));
    bat=new ImageIcon(getClass().getResource("image\\tree\\new\\bat.gif"));
    dll=new ImageIcon(getClass().getResource("image\\tree\\new\\dll.gif"));
    dat=new ImageIcon(getClass().getResource("image\\tree\\new\\dat.gif"));
    javaf=new ImageIcon(getClass().getResource("image\\tree\\new\\java.gif"));
    sys=new ImageIcon(getClass().getResource("image\\tree\\new\\sys.gif"));
    classf=new ImageIcon(getClass().getResource("image\\tree\\new\\class.gif"));
    asf=new ImageIcon(getClass().getResource("image\\tree\\new\\asf.gif"));
    inf=new ImageIcon(getClass().getResource("image\\tree\\new\\inf.gif"));
    png=new ImageIcon(getClass().getResource("image\\tree\\new\\png.gif"));
    }
    public void viewload(File fff,TreePath ttt,JTree jtree)
    {
    removeAll();
    File c=fff;
            File[] files=c.listFiles();
    int i=0,j=0;
    for(int k=0;k<files.length;k++)
    {
    if(files[k].isFile())
    i++;
    else if(files[k].isDirectory())
    j++;
    }
    f=new File[i];
    d=new File[j];
    i=0;j=0;
    for(int k=0;k<files.length;k++)
    {
    if(files[k].isFile())
    {
     f[i]=files[k];
     i++;
    }
    else if(files[k].isDirectory())
    {
    d[j]=files[k];
    j++;
    }
    }
    setLayout(new GridLayout((int)(files.length/6)+1,6));
    for(int a=0;a<j;a++)
    add(new FileIcon(d[a],ttt,jtree,a));
    for(int b=0;b<i;b++)
    add(new FileIcon(f[b],ttt,jtree,b));
    }
    /*
    public void reviewload(File fff)
    {
    viewload(fff); }
    */
    //*********************FileIcon
    class FileIcon extends JLabel
    {
    public FileIcon(final File str,final TreePath path,final JTree jtree,final int i)
    {
    super(str.getName());
    setToolTipText(str.getName());
    setFont(new Font("黑体",Font.PLAIN,12));
    setForeground(Color.gray);
    setHorizontalTextPosition(SwingConstants.CENTER);
    setVerticalTextPosition(SwingConstants.BOTTOM);
    setHorizontalAlignment(SwingConstants.CENTER);
    //setVerticalAlignment(SwingConstants.BOTTOM);
    setPreferredSize(new Dimension(40,70));
    if(str.isDirectory())
    {
    setIcon(icod);
    addMouseListener(
    new MouseListener()
    {
    public void mouseEntered(MouseEvent e)
    {
    FileIcon.this.setBorder(new TitledBorder("Directory"));
    ((TitledBorder)(FileIcon.this.getBorder())).setTitleColor(Color.blue);
    FileIcon.this.repaint();
    enterfile.play();
    }
    public void mouseExited(MouseEvent e)
    {
    FileIcon.this.setBorder(null);
    FileIcon.this.repaint();
    }
    public void mousePressed(MouseEvent e)
    {
    }
    public void mouseReleased(MouseEvent e)
    {
    }
    public void mouseClicked(MouseEvent e)
    {
    if(e.getClickCount()==2)
    {
    ViewPanel.this.viewload(str,path,jtree);
    opendir.play();
    jtree.expandPath(path);
    treeNode n=(treeNode)(path.getLastPathComponent());
    treeNode n2=(treeNode)(n.getChildAt(i));
    jtree.setSelectionPath(new TreePath(n2.getPath()));
    //jtree.repaint();
    //jtree.validate();
    if(sss!=null)
    sss.validate();
    }
    }
    });
    }
    else
    {
    String name=getPostfix(str.getName());
    //System.out.println(name);
    if(name.equals("zip") || name.equals("rar"))
    setIcon(zip);
    else if(name.equals("doc"))
    setIcon(doc);
    else if(name.equals("exe"))
    setIcon(exe);
    else if(name.equals("ttf") || name.equals("fon"))
    setIcon(fon);
    else if(name.equals("gif"))
    {
    setIcon(gif);
    }
    else if(name.equals("htm") || name.equals("html"))
    {
    setIcon(htm);
    }
    else if(name.equals("jpg"))
    setIcon(jpg);
    else if(name.equals("bmp"))
    setIcon(bmp);
    else if(name.equals("log"))
    setIcon(log);
    else if(name.equals("eml"))
    setIcon(eml);
    else if(name.equals("mid") || name.equals("mp3"))
    setIcon(music);
    else if(name.equals("pdf"))
    setIcon(pdf);
    else if(name.equals("scr"))
    setIcon(scr);
    else if(name.equals("txt"))
    setIcon(txt);
    else if(name.equals("rm") || name.equals("avi"))
    setIcon(vedio);
    else if(name.equals("wav"))
    setIcon(wav);
    else if(name.equals("bat"))
    setIcon(bat);
    else if(name.equals("dll"))
    setIcon(dll);
    else if(name.equals("dat"))
    setIcon(dat);
    else if(name.equals("java"))
    setIcon(javaf);
    else if(name.equals("sys"))
    setIcon(sys);
    else if(name.equals("class"))
    setIcon(classf);
    else if(name.equals("asf"))
    setIcon(asf);
    else if(name.equals("inf"))
    setIcon(inf);
    else if(name.equals("png"))
    setIcon(png);
    else
    setIcon(def);
    addMouseListener(
    new MouseListener()
    {
    public void mouseEntered(MouseEvent e)
    {
    FileIcon.this.setBorder(new TitledBorder("File"));
    ((TitledBorder)(FileIcon.this.getBorder())).setTitleColor(Color.red);
    FileIcon.this.repaint();
    enterfile.play();
    }
    public void mouseExited(MouseEvent e)
    {
    FileIcon.this.setBorder(null);
    FileIcon.this.repaint();
    }
    public void mousePressed(MouseEvent e)
    {
    }
    public void mouseReleased(MouseEvent e)
    {
    }
    public void mouseClicked(MouseEvent e)
    {
    }
    });
    }
    }
    public String getPostfix(String str)
    {
    int index=str.lastIndexOf(".");
    String s;
    if(index!=-1)
    {
    s=str.substring(index+1);
    }
    else
    {
    s="???...";
    }
    //System.out.println(s);
    return s.toLowerCase();
    }
    }
    }
      

  2.   

    天呀,那么长代码查一下JAVA文档吧