博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Swing tree例子

Posted on 2012-05-15 23:36  紫冰龙  阅读(363)  评论(0编辑  收藏  举报
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

import java.awt.*;
import java.io.File;
public class TreeDemo extends JFrame {
    JTree tree;
    public TreeDemo(){
        super("A tree");
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        createtree();
        add(new JScrollPane(tree));
        setVisible(true);
        
    }
    public void createtree() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new treenode("E:\\NETBACKUP\\文档"));
        
        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(new treenode("E:\\NETBACKUP\\文档\\文档刷新"));
        DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(new treenode("E:\\NETBACKUP\\文档\\文档刷新\\Centralized Backup Installation Manual.doc"));
        DefaultMutableTreeNode node3 = new DefaultMutableTreeNode(new treenode("E:\\NETBACKUP\\文档\\文档刷新\\Centralized Backup Installation Manual.doc"));
        root.add(node1);
        //root.add(node1);
        node1.add(node2);
        node2.add(node3);
        tree = new JTree(root);
        tree.setEditable(true);

    }
    public static void main(String[] args) {
        new TreeDemo();
    }
    
}
class treenode extends File{
    public treenode(String name){
        super(name);
        
    }
    public String toString() {
        return getName();
    }
}