怎样用JTree生成文件目录树?就像window中资源管理器的树一样 怎样用JTree生成文件目录树?就像window中资源管理器的树一样.高手来,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 LZ,TreeModel/Renderer会用吗,如果会用的话,用FileSystemView import java.awt.BorderLayout;import java.awt.Component;import java.io.File;import java.util.*;import javax.swing.*;import javax.swing.border.EmptyBorder;import javax.swing.filechooser.FileSystemView;import javax.swing.tree.DefaultTreeCellRenderer;import javax.swing.tree.TreeNode;/** * @author Kirill Grouchnikov */public class FileTreePanel extends JPanel { /** * File system view. */ protected static FileSystemView fsv = FileSystemView.getFileSystemView(); /** * Renderer for the file tree. * * @author Kirill Grouchnikov */ private static class FileTreeCellRenderer extends DefaultTreeCellRenderer { /** * Icon cache to speed the rendering. */ private Map<String, Icon> iconCache = new HashMap<String, Icon>(); /** * Root name cache to speed the rendering. */ private Map<File, String> rootNameCache = new HashMap<File, String>(); /* * (non-Javadoc) * * @see javax.swing.tree.DefaultTreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, * java.lang.Object, boolean, boolean, boolean, int, boolean) */ @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { FileTreeNode ftn = (FileTreeNode) value; File file = ftn.file; String filename = ""; if (file != null) { if (ftn.isFileSystemRoot) { // long start = System.currentTimeMillis(); filename = this.rootNameCache.get(file); if (filename == null) { filename = fsv.getSystemDisplayName(file); this.rootNameCache.put(file, filename); } // long end = System.currentTimeMillis(); // System.out.println(filename + ":" + (end - start)); } else { filename = file.getName(); } } JLabel result = (JLabel) super.getTreeCellRendererComponent(tree, filename, sel, expanded, leaf, row, hasFocus); if (file != null) { Icon icon = this.iconCache.get(filename); if (icon == null) { // System.out.println("Getting icon of " + filename); icon = fsv.getSystemIcon(file); this.iconCache.put(filename, icon); } result.setIcon(icon); } return result; } } /** * A node in the file tree. * * @author Kirill Grouchnikov */ private static class FileTreeNode implements TreeNode { /** * Node file. */ private File file; /** * Children of the node file. */ private File[] children; /** * Parent node. */ private TreeNode parent; /** * Indication whether this node corresponds to a file system root. */ private boolean isFileSystemRoot; /** * Creates a new file tree node. * * @param file * Node file * @param isFileSystemRoot * Indicates whether the file is a file system root. * @param parent * Parent node. */ public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) { this.file = file; this.isFileSystemRoot = isFileSystemRoot; this.parent = parent; this.children = this.file.listFiles(); if (this.children == null) this.children = new File[0]; } /** * Creates a new file tree node. * * @param children * Children files. */ public FileTreeNode(File[] children) { this.file = null; this.parent = null; this.children = children; } /* * (non-Javadoc) * * @see javax.swing.tree.TreeNode#children() */ public Enumeration<?> children() { final int elementCount = this.children.length; return new Enumeration<File>() { int count = 0; /* * (non-Javadoc) * * @see java.util.Enumeration#hasMoreElements() */ public boolean hasMoreElements() { return this.count < elementCount; } /* * (non-Javadoc) * * @see java.util.Enumeration#nextElement() */ public File nextElement() { if (this.count < elementCount) { return FileTreeNode.this.children[this.count++]; } throw new NoSuchElementException("Vector Enumeration"); } }; } /* * (non-Javadoc) * * @see javax.swing.tree.TreeNode#getAllowsChildren() */ public boolean getAllowsChildren() { return true; } /* * (non-Javadoc) * * @see javax.swing.tree.TreeNode#getChildAt(int) */ public TreeNode getChildAt(int childIndex) { return new FileTreeNode(this.children[childIndex], this.parent == null, this); } /* * (non-Javadoc) * * @see javax.swing.tree.TreeNode#getChildCount() */ public int getChildCount() { return this.children.length; } /* * (non-Javadoc) * * @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode) */ public int getIndex(TreeNode node) { FileTreeNode ftn = (FileTreeNode) node; for (int i = 0; i < this.children.length; i++) { if (ftn.file.equals(this.children[i])) return i; } return -1; } /* * (non-Javadoc) * * @see javax.swing.tree.TreeNode#getParent() */ public TreeNode getParent() { return this.parent; } /* * (non-Javadoc) * * @see javax.swing.tree.TreeNode#isLeaf() */ public boolean isLeaf() { return (this.getChildCount() == 0); } } /** * The file tree. */ private JTree tree; /** * Creates the file tree panel. */ public FileTreePanel() { this.setLayout(new BorderLayout()); File[] roots = File.listRoots(); FileTreeNode rootTreeNode = new FileTreeNode(roots); this.tree = new JTree(rootTreeNode); this.tree.setCellRenderer(new FileTreeCellRenderer()); this.tree.setRootVisible(false); final JScrollPane jsp = new JScrollPane(this.tree); jsp.setBorder(new EmptyBorder(0, 0, 0, 0)); this.add(jsp, BorderLayout.CENTER); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("File tree"); frame.setSize(500, 400); frame.setLocationRelativeTo(null); frame.add(new FileTreePanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }} Kirill Grouchnikov就是他,当年我就是下载他的例子的。quite a long time ago 用Jbuilder 可视化 直接拖个tree出来。 把你的Tree定义好,用这个方法就可以了public void setTree(String Path, DefaultMutableTreeNode TreePath) { // 递归遍历目录树 try { File source = new File(Path);// 得到源文件路径 String[] fileName = source.list();// 的到该目录下文件列表 for (int i = 0; i < fileName.length; i++) {// 显示选择了的文件列表 File fileMem = new File(source.getPath(), fileName[i]); if (fileMem.isDirectory()) {// 是目录则添加目录 DefaultMutableTreeNode TreeMem = new DefaultMutableTreeNode( fileMem.getName()); TreePath.add(TreeMem);// 在树中添加目录 setTree(fileMem.getPath(), TreeMem);// 递归遍历目录树 }// end if else {// 是文件则添加节点 DefaultMutableTreeNode TreeMem = new DefaultMutableTreeNode( fileMem.getName()); TreePath.add(TreeMem);// 添加节点 } }// end for }// End try catch (Exception ei) { ei.printStackTrace(); JOptionPane.showMessageDialog(this, "系统出错:" + ei);// 显示提示信息。 } } 编写代码时遇到的一个问题:编写学生成绩计算类,计算学生平均分 ... 为什么类成员方法只能访问类成员变量,而不能访问实例成员变量? 用dos命令编译java包中的所有文件(在线等) 图像交点算法问题求助 要去深圳工作了...散点分...伤心.. 有什麽简单方法可以复制一颗树? java实现zip压缩的问题 corejava 疑惑 在CORBA中,如何处理多个请求 为什么我的Jsp文件脱离JBuilder就不能运行?(也就是不能在浏览器中运行) 一些小问题 关于Java的多线程编程
import java.awt.Component;
import java.io.File;
import java.util.*;import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;/**
* @author Kirill Grouchnikov
*/
public class FileTreePanel extends JPanel {
/**
* File system view.
*/
protected static FileSystemView fsv = FileSystemView.getFileSystemView(); /**
* Renderer for the file tree.
*
* @author Kirill Grouchnikov
*/
private static class FileTreeCellRenderer extends DefaultTreeCellRenderer {
/**
* Icon cache to speed the rendering.
*/
private Map<String, Icon> iconCache = new HashMap<String, Icon>(); /**
* Root name cache to speed the rendering.
*/
private Map<File, String> rootNameCache = new HashMap<File, String>(); /*
* (non-Javadoc)
*
* @see javax.swing.tree.DefaultTreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
* java.lang.Object, boolean, boolean, boolean, int, boolean)
*/
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
FileTreeNode ftn = (FileTreeNode) value;
File file = ftn.file;
String filename = "";
if (file != null) {
if (ftn.isFileSystemRoot) {
// long start = System.currentTimeMillis();
filename = this.rootNameCache.get(file);
if (filename == null) {
filename = fsv.getSystemDisplayName(file);
this.rootNameCache.put(file, filename);
}
// long end = System.currentTimeMillis();
// System.out.println(filename + ":" + (end - start));
} else {
filename = file.getName();
}
}
JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,
filename, sel, expanded, leaf, row, hasFocus);
if (file != null) {
Icon icon = this.iconCache.get(filename);
if (icon == null) {
// System.out.println("Getting icon of " + filename);
icon = fsv.getSystemIcon(file);
this.iconCache.put(filename, icon);
}
result.setIcon(icon);
}
return result;
}
} /**
* A node in the file tree.
*
* @author Kirill Grouchnikov
*/
private static class FileTreeNode implements TreeNode {
/**
* Node file.
*/
private File file; /**
* Children of the node file.
*/
private File[] children; /**
* Parent node.
*/
private TreeNode parent; /**
* Indication whether this node corresponds to a file system root.
*/
private boolean isFileSystemRoot; /**
* Creates a new file tree node.
*
* @param file
* Node file
* @param isFileSystemRoot
* Indicates whether the file is a file system root.
* @param parent
* Parent node.
*/
public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) {
this.file = file;
this.isFileSystemRoot = isFileSystemRoot;
this.parent = parent;
this.children = this.file.listFiles();
if (this.children == null)
this.children = new File[0];
} /**
* Creates a new file tree node.
*
* @param children
* Children files.
*/
public FileTreeNode(File[] children) {
this.file = null;
this.parent = null;
this.children = children;
} /*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#children()
*/
public Enumeration<?> children() {
final int elementCount = this.children.length;
return new Enumeration<File>() {
int count = 0; /*
* (non-Javadoc)
*
* @see java.util.Enumeration#hasMoreElements()
*/
public boolean hasMoreElements() {
return this.count < elementCount;
} /*
* (non-Javadoc)
*
* @see java.util.Enumeration#nextElement()
*/
public File nextElement() {
if (this.count < elementCount) {
return FileTreeNode.this.children[this.count++];
}
throw new NoSuchElementException("Vector Enumeration");
}
}; } /*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getAllowsChildren()
*/
public boolean getAllowsChildren() {
return true;
} /*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getChildAt(int)
*/
public TreeNode getChildAt(int childIndex) {
return new FileTreeNode(this.children[childIndex],
this.parent == null, this);
} /*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getChildCount()
*/
public int getChildCount() {
return this.children.length;
} /*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode)
*/
public int getIndex(TreeNode node) {
FileTreeNode ftn = (FileTreeNode) node;
for (int i = 0; i < this.children.length; i++) {
if (ftn.file.equals(this.children[i]))
return i;
}
return -1;
} /*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getParent()
*/
public TreeNode getParent() {
return this.parent;
} /*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#isLeaf()
*/
public boolean isLeaf() {
return (this.getChildCount() == 0);
}
} /**
* The file tree.
*/
private JTree tree; /**
* Creates the file tree panel.
*/
public FileTreePanel() {
this.setLayout(new BorderLayout()); File[] roots = File.listRoots();
FileTreeNode rootTreeNode = new FileTreeNode(roots);
this.tree = new JTree(rootTreeNode);
this.tree.setCellRenderer(new FileTreeCellRenderer());
this.tree.setRootVisible(false);
final JScrollPane jsp = new JScrollPane(this.tree);
jsp.setBorder(new EmptyBorder(0, 0, 0, 0));
this.add(jsp, BorderLayout.CENTER);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("File tree");
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.add(new FileTreePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
就是他,当年我就是下载他的例子的。quite a long time ago
public void setTree(String Path, DefaultMutableTreeNode TreePath) {
// 递归遍历目录树
try {
File source = new File(Path);// 得到源文件路径
String[] fileName = source.list();// 的到该目录下文件列表
for (int i = 0; i < fileName.length; i++) {// 显示选择了的文件列表
File fileMem = new File(source.getPath(), fileName[i]);
if (fileMem.isDirectory()) {// 是目录则添加目录
DefaultMutableTreeNode TreeMem = new DefaultMutableTreeNode(
fileMem.getName());
TreePath.add(TreeMem);// 在树中添加目录
setTree(fileMem.getPath(), TreeMem);// 递归遍历目录树
}// end if
else {// 是文件则添加节点
DefaultMutableTreeNode TreeMem = new DefaultMutableTreeNode(
fileMem.getName());
TreePath.add(TreeMem);// 添加节点
}
}// end for
}// End try
catch (Exception ei) {
ei.printStackTrace();
JOptionPane.showMessageDialog(this, "系统出错:" + ei);// 显示提示信息。
}
}