如何实现一个文件目录的树形显示?

xiaming 2001-08-08 09:29:36
就象ie中整理收藏夹的功能~~~~~可以在这个根目录下可以添加文件夹?删除文件夹等
...全文
280 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
longaway 2001-08-15
  • 打赏
  • 举报
回复

主要是比较累。
用 java 删文件夹,就是要把文件都删了,再删空目录。
对 java 文件和目录是差不多的,
都是先声明一个 File ,再用 file.delete(); 。
在树上加一个右键事件,弹出一个菜单。
如果你用 ide ,
应该是很方便的。

自己努力一下吧。

xiaming 2001-08-10
  • 打赏
  • 举报
回复
很难?还是懒的说?
xiaming 2001-08-10
  • 打赏
  • 举报
回复
楼上的谢谢了,不知道还有哪位能完善一下功能,实现在这个树上添加删除文件夹功能?
xiaming 2001-08-10
  • 打赏
  • 举报
回复
哪位大侠出手啊???救命啊~~~~~~~~~~
xuxk 2001-08-09
  • 打赏
  • 举报
回复
我想可以用File类的list()方法遍厉目录及文件,用JTree实现相应的目录结构。
然后添加和删除就是事件和文件操作了
longaway 2001-08-09
  • 打赏
  • 举报
回复
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.EventObject;

public class Test extends JFrame {
public Test() {
final JTree tree = new JTree(createTreeModel());
JScrollPane scrollPane = new JScrollPane(tree);

getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(GJApp.getStatusArea(),
BorderLayout.SOUTH);

tree.addTreeExpansionListener(new TreeExpansionListener(){
public void treeCollapsed(TreeExpansionEvent e) {
}
public void treeExpanded(TreeExpansionEvent e) {
UpdateStatus updateThread;
TreePath path = e.getPath();
FileNode node = (FileNode)
path.getLastPathComponent();

if( ! node.isExplored()) {
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
GJApp.updateStatus("exploring ...");

UpdateStatus us = new UpdateStatus();
us.start();

node.explore();
model.nodeStructureChanged(node);
}
}
class UpdateStatus extends Thread {
public void run() {
try { Thread.currentThread().sleep(450); }
catch(InterruptedException e) { }

SwingUtilities.invokeLater(new Runnable() {
public void run() {
GJApp.updateStatus(" ");
}
});
}
}
});
}
private DefaultTreeModel createTreeModel() {
File root = new File("E:/");
FileNode rootNode = new FileNode(root);

rootNode.explore();
return new DefaultTreeModel(rootNode);
}
public static void main(String args[]) {
GJApp.launch(new Test(),"JTree File Explorer",
300,300,450,400);
}
}
class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;

public FileNode(File file) {
setUserObject(file);
}
public boolean getAllowsChildren() { return isDirectory(); }
public boolean isLeaf() { return !isDirectory(); }
public File getFile() { return (File)getUserObject(); }

public boolean isExplored() { return explored; }

public boolean isDirectory() {
File file = getFile();
return file.isDirectory();
}
public String toString() {
File file = (File)getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator);

return (index != -1 && index != filename.length()-1) ?
filename.substring(index+1) :
filename;
}
public void explore() {
if(!isDirectory())
return;

if(!isExplored()) {
File file = getFile();
File[] children = file.listFiles();

for(int i=0; i < children.length; ++i)
add(new FileNode(children[i]));

explored = true;
}
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");

public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);

statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);

f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);

f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void updateStatus(String s) {
status.setText(s);
}
}
longaway 2001-08-09
  • 打赏
  • 举报
回复
用 JFileChooser 吧。
或者看一下 JFileChooser.java 。

当然可能太难了。
xiaming 2001-08-09
  • 打赏
  • 举报
回复
大侠能说的具体一点吗?我是菜鸟。给我一段程序我不知道该放在什么地方,能给全程序吗?
skyyoung 2001-08-09
  • 打赏
  • 举报
回复
File[] fs = File.listRoots();
System.out.println("size:"+fs.length);
for(int i=0;i<fs.length;i++)
{
System.out.println(fs[i].getPath());
String[] files = fs[i].list();
System.out.println(files.length);
}
xiaming 2001-08-09
  • 打赏
  • 举报
回复
具体如何实现呢?

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧