动态改变树节点图标和遍历树


public class TreeDemo5 extends JFrame{
 /**
  * 动态改变树的图标
  */
 JTree   tree ;
 TreeDemo5(){
  try{
   this.setTitle("动态改变树的图标");
   this.setBounds(100, 100, 400, 400);
   this.setLayout(new GridLayout(1,2));
   JPanel panel = new JPanel();
   panel.setBounds(10, 10, 200, 400);
   
   
   DefaultMutableTreeNode root = new DefaultMutableTreeNode(new NodeDatas(3 , "数据库导航",1));
   DefaultMutableTreeNode salaryDb = new DefaultMutableTreeNode(new NodeDatas(3 , "公司工资数据库",2));  
      DefaultMutableTreeNode customerDb = new DefaultMutableTreeNode(new NodeDatas(3 , "公司客户数据库",3));  
      //定义salaryDb的两个子节点  
      DefaultMutableTreeNode employee = new DefaultMutableTreeNode(new NodeDatas(3 , "员工表",4));  
      DefaultMutableTreeNode attend = new DefaultMutableTreeNode(new NodeDatas(3 , "考勤表",5));  
      //定义customerDb的一个子节点  
      DefaultMutableTreeNode contact = new DefaultMutableTreeNode(new NodeDatas(3, "联系方式表",6));  
   
      //定义employee的三个子节点  
      DefaultMutableTreeNode id = new DefaultMutableTreeNode(new NodeDatas(0, "员工ID",7));  
      DefaultMutableTreeNode name = new DefaultMutableTreeNode(new NodeDatas(0 , "姓名",8));  
      DefaultMutableTreeNode gender = new DefaultMutableTreeNode(new NodeDatas(1 , "性别",9));
     
     
      root.add(salaryDb);  
         root.add(customerDb);  
         salaryDb.add(employee);  
         salaryDb.add(attend);  
         customerDb.add(contact);  
         employee.add(id);  
         employee.add(name);  
         employee.add(gender);  
        
         for(int i = 0 ; i < 5000 ; i++){
          if(i+10==500){
           employee.add(new DefaultMutableTreeNode(new NodeDatas(1,"这里是测试的第500个数据",i+10)));
          }else if(i+10==10){
           employee.add(new DefaultMutableTreeNode(new NodeDatas(1,"这里是测试的第10个数据",i+10)));
          }else{
           employee.add(new DefaultMutableTreeNode(new NodeDatas(1,"姓名",i+10)));
          }
      }
        
         //以根节点创建树  
         tree = new JTree(root);  
        
         //设置该JTree使用自定义的节点绘制器  
         tree.setCellRenderer(new setTreeIcon());  
       
         //设置是否显示根节点的“展开/折叠”图标,默认是false  
         tree.setShowsRootHandles(true);  
         //设置节点是否可见,默认是true  
         tree.setRootVisible(true);  
  
         //设置使用Windows风格外观  
         UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
        
         //更新JTree的UI外观  
         SwingUtilities.updateComponentTreeUI(tree);
         //设计树可编辑模式
         tree.setEditable(true);
        
        
        
         JButton jb1 = new JButton("编辑");
         jb1.addActionListener(new ActionListener()  
         {  
             public void actionPerformed(ActionEvent event)  
             {  
              DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
              if(node!=null){
               NodeDatas nd = (NodeDatas)node.getUserObject();
               
               //修改当前节点状态
               if(nd.getNodeType()==0){
                nd.setNodeType(1);
               }else{
                nd.setNodeType(0);
               }
 //              更新JTree的UI外观  
               SwingUtilities.updateComponentTreeUI(tree);
              }
             }  
         }); 
         panel.add(tree);
         JScrollPane js = new JScrollPane(panel);
         js.setBounds(10, 10, 200, 400);
         JPanel panel2 = new JPanel();
         panel2.setLayout(new GridLayout(2,1));
         panel2.add(jb1);
        
         JButton jb2 = new JButton("遍历");
         jb2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
     this.forTree();
    }
    
    /***
     * 遍历树
     *
     */
    public void forTree(){
     //获得根节点
     DefaultMutableTreeNode root  = (DefaultMutableTreeNode)tree.getModel().getRoot();
     
     Enumeration   e   =   root.breadthFirstEnumeration();
     
     //遍历树
     while(e.hasMoreElements())  
     {  
      //获得当前节点
      DefaultMutableTreeNode   node   =   (DefaultMutableTreeNode)e.nextElement();
      //转换成实体类对象
      NodeDatas nds = (NodeDatas)node.getUserObject();
      
      //判断是否是需要改变的节点
      if(nds.getUid()==10){
       System.out.println("这里是ID"+nds.getUid()+":"+nds.getNodeData());
       if(nds.getNodeType()==0){
                 nds.setNodeType(1);
                }else{
                 nds.setNodeType(0);
                }
//                更新JTree的UI外观  
                SwingUtilities.updateComponentTreeUI(tree);
      }
     }
    }
          
         });
         panel2.add(jb2);
   this.add(js);
   this.add(panel2);
   this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent arg0) {
     System.exit(0);
    }
    
   });
   this.show();
  }catch(Exception ex){
   ex.printStackTrace();
  }
 }
 
 public void forTree(){
  DefaultMutableTreeNode root  = (DefaultMutableTreeNode)tree.getModel().getRoot();
  Enumeration   e   =   root.breadthFirstEnumeration();  
  while(e.hasMoreElements())  
  {  
   DefaultMutableTreeNode   node   =   (DefaultMutableTreeNode)e.nextElement();
   NodeDatas nds = (NodeDatas)node.getUserObject();
   System.out.println(nds.getUid());
  }
 }
 
 
 public static void main(String [] args){
  new TreeDemo5();
 }
}

 

//定义一个NodeData类,用于封装节点数据  
class NodeDatas  
{  
    public int nodeType;  
    public int uid;
    public String nodeData;  
    public NodeDatas(int nodeType , String nodeData, int uid)  
    {  
        this.nodeType = nodeType;  
        this.nodeData = nodeData;  
        this.uid = uid;
    }  
    public String toString()  
    {  
        return nodeData;  
    }
 public String getNodeData() {
  return nodeData;
 }
 public void setNodeData(String nodeData) {
  this.nodeData = nodeData;
 }
 public int getNodeType() {
  return nodeType;
 }
 public void setNodeType(int nodeType) {
  this.nodeType = nodeType;
 }
 public int getUid() {
  return uid;
 }
 public void setUid(int uid) {
  this.uid = uid;
 }  
}

/**
 * 树节点渲染器
 * @author: sitinspring(junglesong@gmail.com)
 * @date: 2008-2-12
 */
class setTreeIcon extends DefaultTreeCellRenderer{
 /**
  * 加载树节点图标
  */

 int i =1;
   
    // 數據庫圖標,頂層節點用
    private static final Icon databaseIcon = new ImageIcon("D://images//1.gif");
   
    // 表圖標,第三層節點用
    private static final Icon tableIcon = new ImageIcon("D://images//2.gif"); 
   
    // 表空間關閉狀態圖標,關閉狀態的第二層節點用
    private static final Icon tableSpaceCloseIcon = new ImageIcon("D://images//3.gif"); 
   
    // 表空間關閉狀態圖標,打開狀態的第二層節點用
    private static final Icon tableSpaceOpenIcon = new ImageIcon("D://images//4.gif"); 
   
    public Component getTreeCellRendererComponent(JTree tree,Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus){
         super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);  
        // 取得节点
         DefaultMutableTreeNode node = (DefaultMutableTreeNode)value; 
         NodeDatas data = (NodeDatas)node.getUserObject();
       
        // 取得路径
        TreeNode[] paths = node.getPath();
       
        // 按路径层次赋予不同的图标
        if (paths.length == 4) {
         if(data.nodeType==1){
          setIcon(tableIcon);
         }else{
          setIcon(tableSpaceOpenIcon);
         }
        }else if(paths.length == 3){
//            //按展开情况再赋予不同的图标
//            if(expanded){
//                setIcon(tableSpaceOpenIcon);
//            }
//            else{
                setIcon(tableSpaceCloseIcon);
//            }
        }else if(paths.length == 2){
              setIcon(databaseIcon);
        }
        else if(paths.length == 1){
            setIcon(databaseIcon);
        }
        return this;       
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值