# jtree filebrowser



## gast (6. Jan 2004)

Kennt jemand ein Tutorial von einem jtree Filebrowser?
Vielen Dank.


----------



## Math55 (6. Jan 2004)

sowas:


```
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import com.sun.java.swing.*;
import com.sun.java.swing.tree.*;
import com.sun.java.swing.event.*;

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();
  }
}


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);
  }  
}
```

gruß math55


----------



## Ebenius (7. Jan 2004)

:!: Zum vorangegangen Code-Beispiel ein Tipp, der mit dem Problem nichts zu tun hat: Du brauchst nicht extra einen WindowListener auf das JFrame zu setzen, nur um beim Schließen die Applikation zu beenden. Das wird häufig getan ist aber etwas viel Code für diese Kleinigkeit. Aus diesem Grund hat sun im JDK 1.2 die Methode JFrame.setDefaultCloseOperation(int) eingeführt.


```
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
```

Es grüßt Ebenius


----------



## Math55 (7. Jan 2004)

guter tip! danke :lol:


----------

