Verzeichnisstruktur als Baum?
-
hallo, wie kann ich grafisch alle verzeichnisse inkl. aller dateien in einer verzeichnisstruktur ausgeben, so wie beim explorer? danke:)
-
Mit Google wird man schnell fündig
einfach mal die Wörter java directory JTree eingeben und schwupps kommt das hier
[java]/**
* Java Directory Browser.
*
* Swing example of a dynamically-built JTree,
* using a file system hierarchy as a data model.
* (Tested with Swing 1.02)
*/import java.awt.;
import java.awt.event.;
import java.io.;
import java.util.;
import javax.swing.;
import javax.swing.tree.;
import javax.swing.event.*;/**
* A TreeNode that contains a File object
*/
class FileTreeNode extends DefaultMutableTreeNode {
boolean hasbeenread;public FileTreeNode(File file) {
super(file);
hasbeenread = false;
}public boolean readTree() {
return readTree(false);
}public boolean readTree(boolean b) {
if(hasbeenread)
return false;String list[] = getFileObject().list();
if(list!= null) {
for(int i=0; i<list.length;i++) {
FileTreeNode subnode = new FileTreeNode(new File(getFileObject(),
list[i]));
add(subnode);
if(b)
subnode.readTree(b);
}
}
hasbeenread=true;
return true;
}public File getFileObject() {
return (File)getUserObject();
}public String toString() {
return getFileObject().getName();
}public boolean isLeaf() {
return ((File)userObject).isFile();
}
}/**
* Listens for expansion events
*/
class HierarchyBrowserUpdater implements TreeExpansionListener {
FileTreeNode root;
JTree jtree;
DefaultTreeModel treemodel;
Frame frame;public HierarchyBrowserUpdater(JTree tree, FileTreeNode node,
DefaultTreeModel tmodel, Frame theFrame) {
root = node;
jtree = tree;
treemodel = tmodel;
frame = theFrame;
}public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
System.out.print("Received expansion event on ");
System.out.println(path);FileTreeNode node = (FileTreeNode)path.getLastPathComponent();
System.out.println("Node level: " + node.getLevel());
System.out.println("Children " + node.getChildCount());
System.out.println("Reading subtree " + node.toString());frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
frame.setEnabled(false);if(node.readTree()) {
System.out.println("Calling nodesWereInserted");
int childrenIdx[] = new int[node.getChildCount()];
int i=0;
for(Enumeration e = node.children(); e.hasMoreElements();) {
Object obj = e.nextElement();
childrenIdx[i] = node.getIndex((TreeNode)obj);
i++;
}
treemodel.nodesWereInserted(node, childrenIdx);
}
frame.setEnabled(true);
frame.setCursor(Cursor.getDefaultCursor());
}public void treeCollapsed(TreeExpansionEvent event) {
}
}/**
* Shows a tree structure (of a directory)
*/
public class TreeFrame extends Frame implements WindowListener {public TreeFrame(String dir) {
setLayout(new BorderLayout());
addNotify();
setSize(getInsets().left + getInsets().right + 485,
getInsets().top + getInsets().bottom + 367);
setTitle("Java Directory Browser");FileTreeNode root = (FileTreeNode)buildTree(dir);
DefaultTreeModel treemodel = new DefaultTreeModel(root);
JTree tree = new JTree(treemodel);
tree.setDoubleBuffered(true);HierarchyBrowserUpdater upd = new HierarchyBrowserUpdater(tree,
root,
treemodel,
this);
tree.addTreeExpansionListener(upd);JScrollPane scrollpane = new JScrollPane();
scrollpane.getViewport().add(tree);
scrollpane.setDoubleBuffered(true);add("Center", scrollpane);
addWindowListener(this);
}public TreeNode buildTree(String dir) {
FileTreeNode root = new FileTreeNode(new File(dir));
root.readTree(false);
return (TreeNode)root;
}public synchronized void setVisible(boolean show) {
setLocation(50, 50);
super.setVisible(show);
}public void windowClosed(WindowEvent event) {
}public void windowDeiconified(WindowEvent event) {
}public void windowIconified(WindowEvent event) {
}public void windowActivated(WindowEvent event) {
}public void windowDeactivated(WindowEvent event) {
}public void windowOpened(WindowEvent event) {
}public void windowClosing(WindowEvent event) {
System.exit(0);
}public static void main(String argv[]) {
String dir = System.getProperty("user.dir");
if(argv!=null && argv.length==1)
dir = argv[0];
TreeFrame myframe = new TreeFrame(dir);
myframe.setVisible(true);
}
}[/code]
-