Topic: 急求:在java中没有有显示系统文件目录树的控件

  Print this page

1.急求:在java中没有有显示系统文件目录树的控件 Copy to clipboard
Posted by: hikelee
Posted on: 2004-10-25 16:48

各位大侠,在java中有没有显示系统文件目录树的控件

2.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-10-25 21:22

好像没有,你自己做一个吧,应该不难的。

3.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: hikelee
Posted on: 2004-11-05 10:56

呵呵,谢谢,应该不难的.不过自己做的总不如系统自带的控件高效些

4.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: vssivl
Posted on: 2004-11-05 18:02

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
    }
  }
}

5.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: vssivl
Posted on: 2004-11-05 18:03

package gui;

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

public class FileTreeTest {
  public static void main(String[] args) {
    try {
      if (args.length != 1) {
        System.out.println("Please supply one initial directory");
        System.exit(0);
      }
      JFrame f = new JFrame("File Tree Test");
      final FileTree ft = new FileTree(args[0]);

      ft.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent evt) {
          TreePath path = evt.getPath();
          String name = ft.getPathName(path);
          File file = ft.getFile(path);
          System.out.println("File " + name + " has been "
            + (evt.isAddedPath() ? "selected" : "deselected"));
          System.out.println("File object is " + file);
        }
      });

      f.getContentPane().add(new JScrollPane(ft));
      f.setSize(300, 300);
      f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
          System.exit(0);
        }
      });
      f.setVisible(true);
    } catch (FileNotFoundException e) {
      System.out.println("File " + args[0] + " not found");
    }
  }
}

6.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: vssivl
Posted on: 2004-11-05 18:07

这是JFC核心编程上的例子,英国人写的,你可以放心用。

7.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: hikelee
Posted on: 2004-11-05 23:15

哇,真的太谢谢了,确实不错,速度也挺快的,不过就是有一点,这个目录树只能显示文件夹,不能显示文件,有点遗憾啊!

8.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: hikelee
Posted on: 2004-11-06 00:02

噢,我明白了,在上面的代码上稍作修改就可显示文件和文件夹一块显示了,谢谢了.

package untitled6;

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
{
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
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;
}
}
else
{
FileTreeNode node = new FileTreeNode(file, name);
this.add(node);
}
} 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;
}
}

// 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
}
}
}

9.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-06 09:22

很不错,运行良好

10.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: floater
Posted on: 2004-11-07 03:08

http://java.sun.com/products/jfc/tsc/articles/jtree/

11.Re:急求:在java中没有有显示系统文件目录树的控件 [Re: hikelee] Copy to clipboard
Posted by: vssivl
Posted on: 2004-11-08 10:57

别客气,大家一起拿来主义。


   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