Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:急求:在java中没有有显示系统文件目录树的控件 [Re:hikelee]
vssivl

克斯



发贴: 20
积分: 0
于 2004-11-05 18:02 user profilesend a private message to usersend email to vssivlsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
package gui;

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

public class FileTree extends JTree {
  public FileTree(String path) throws FileNotFoundException, SecurityException {
    super((TreeModel)null);      // Create the JTree itself

    // Use horizontal and vertical lines
    putClientProperty("JTree.lineStyle", "Angled");

    // Create the first node
    FileTreeNode rootNode = new FileTreeNode(null, path);
    
    // Populate the root node with its subdirectories
    boolean addedNodes = rootNode.populateDirectories(true);
    setModel(new DefaultTreeModel(rootNode));

    // Listen for Tree Selection Events
    addTreeExpansionListener(new TreeExpansionHandler());
  }

  // Returns the full pathname for a path, or null if not a known path
  public String getPathName(TreePath path) {
    Object o = path.getLastPathComponent();
    if (o instanceof FileTreeNode) {
      return ((FileTreeNode)o).file.getAbsolutePath();
    }
    return null;
  }

  // Returns the File for a path, or null if not a known path
  public File getFile(TreePath path) {
    Object o = path.getLastPathComponent();
    if (o instanceof FileTreeNode) {
      return ((FileTreeNode)o).file;
    }
    return null;
  }
  
  // Inner class that represents a node in this file system tree
  protected static class FileTreeNode extends DefaultMutableTreeNode {
    public FileTreeNode(File parent, String name)
              throws SecurityException, FileNotFoundException {

      this.name = name;

      // See if this node exists and whether it is a directory
      file = new File(parent, name);
      if (!file.exists()) {
        throw new FileNotFoundException("File " + name + " does not exist");
      }

      isDir = file.isDirectory();

      // Hold the File as the user object.
      setUserObject(file);
    }

    // Override isLeaf to check whether this is a directory
    public boolean isLeaf() {
      return !isDir;
    }

    // Override getAllowsChildren to check whether this is a directory
    public boolean getAllowsChildren() {
      return isDir;
    }

    // For display purposes, we return our own name
    public String toString() {      
      return name;
    }

    // If we are a directory, scan our contents and populate
    // with children. In addition, populate those children
    // if the "descend" flag is true. We only descend once,
    // to avoid recursing the whole subtree.
    // Returns true if some nodes were added
    boolean populateDirectories(boolean descend) {
      boolean addedNodes = false;

      // Do this only once
      if (populated == false) {
        if (interim == true) {
          // We have had a quick look here before:
          // remove the dummy node that we added last time
          removeAllChildren();
          interim = false;          
        }        
        
        String[] names = file.list();    // Get list of contents
        
        // Process the directories
        for (int i = 0; i < names.length; i++) {
          String name = names[i];
          File d = new File(file, name);
          try {
            if (d.isDirectory()) {            
              FileTreeNode node = new FileTreeNode(file, name);
              this.add(node);
              if (descend) {
                node.populateDirectories(false);
              }
              addedNodes = true;
              if (descend == false) {
                // Only add one node if not descending
                break;
              }
            }
          } catch (Throwable t) {
            // Ignore phantoms or access problems
          }          
        }
        
        // If we were scanning to get all subdirectories,
        // or if we found no subdirectories, there is no
        // reason to look at this directory again, so
        // set populated to true. Otherwise, we set interim
        // so that we look again in the future if we need to
        if (descend == true || addedNodes == false) {
          populated = true;          
        } else {
          // Just set interim state
          interim = true;          
        }
      }
      return addedNodes;
    }

    protected File file;    // File object for this node
    protected String name;    // Name of this node
    protected boolean populated;// true if we have been populated
    protected boolean interim;  // true if we are in interim state
    protected boolean isDir;  // true if this is a directory
  }

  // Inner class that handles Tree Expansion Events
  protected class TreeExpansionHandler implements TreeExpansionListener {
    public void treeExpanded(TreeExpansionEvent evt) {
      TreePath path = evt.getPath();      // The expanded path
      JTree tree = (JTree)evt.getSource();  // The tree

      // Get the last component of the path and
      // arrange to have it fully populated.
      FileTreeNode node = (FileTreeNode)path.getLastPathComponent();
      if (node.populateDirectories(true)) {
        ((DefaultTreeModel)tree.getModel()).nodeStructureChanged(node);  
      }
    }

    public void treeCollapsed(TreeExpansionEvent evt) {
      // Nothing to do
    }
  }
}




话题树型展开
人气 标题 作者 字数 发贴时间
9212 急求:在java中没有有显示系统文件目录树的控件 hikelee 26 2004-10-25 16:48
7697 Re:急求:在java中没有有显示系统文件目录树的控件 kavinwang 19 2004-10-25 21:22
7679 Re:急求:在java中没有有显示系统文件目录树的控件 hikelee 31 2004-11-05 10:56
7732 Re:急求:在java中没有有显示系统文件目录树的控件 vssivl 4911 2004-11-05 18:02
7866 Re:急求:在java中没有有显示系统文件目录树的控件 vssivl 2355 2004-11-05 18:03
7727 Re:急求:在java中没有有显示系统文件目录树的控件 vssivl 27 2004-11-05 18:07
7695 Re:急求:在java中没有有显示系统文件目录树的控件 hikelee 55 2004-11-05 23:15
7883 Re:急求:在java中没有有显示系统文件目录树的控件 hikelee 5556 2004-11-06 00:02
7507 Re:急求:在java中没有有显示系统文件目录树的控件 littledeer1974 8 2004-11-06 09:22
7479 Re:急求:在java中没有有显示系统文件目录树的控件 floater 52 2004-11-07 03:08
7934 Re:急求:在java中没有有显示系统文件目录树的控件 vssivl 13 2004-11-08 10:57

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923