Pfad in JTree aufklappen (expandieren)

Abapking

Mitglied
Hallo Leute,

ich habe zur Zeit das Problem, dass ich mir einen JTree zusammengebaut habe, der mit zum Beispiel als Dateibrowser dient. Paralell hierzu soll es auch möglich sein, den Baum mit einem Eingabefeld anzusteuern (d. h. ich gebe zum Beispiel C:\Temp\Test\ ein und dieser Pfad soll dann im Baum aufgeklappt werden).

Hierzu gibt es im JTree die Methode expandPath, welche als Übergabeparamter einen TreePath haben möchte. Meine Frage ist nun, wie ich den Pfad (C:\Temp\Test\) in den TreePath reinbekomme (Format) um dann den Baum anzusteuern...?

Weiss da jemand von euch weiter?
 
Hallo,

schau mal hier:
Java:
/**
 * 
 */
package de.tutorials;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 * @author Tom
 * 
 */
public class JTreeDirectoryView extends JFrame {

  public JTreeDirectoryView() {
    super("JTreeDirectoryView");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    final JTree tree = new JTree();
    buildTreeModelAccordingToDirectoryStructure(new File("c:/"), tree);

    final JTextField txtPath = new JTextField(50);
    txtPath.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (!"".equals(txtPath.getText())) {
          TreePath treePath = createTreePathFor(txtPath.getText());
          tree.expandPath(treePath);
          tree.setSelectionPath(treePath);
          tree.scrollPathToVisible(treePath);
        }
      }
    });
    add(txtPath, BorderLayout.NORTH);
    add(new JScrollPane(tree), BorderLayout.CENTER);

    pack();
    setVisible(true);
  }


  protected TreePath createTreePathFor(String path) {
    File file = new File(path);
    List<FileSystemTreeNode> fileSystemNodes = new ArrayList<FileSystemTreeNode>();
    fileSystemNodes.add(new FileSystemTreeNode(file));
    while (null != file.getParentFile()) {
      fileSystemNodes.add(new FileSystemTreeNode(file = file.getParentFile()));
    }
    Collections.reverse(fileSystemNodes);
    TreePath treePath = new TreePath(fileSystemNodes.toArray());
    return treePath;
  }


  void buildTreeModelAccordingToDirectoryStructure(File file, JTree tree) {
    tree.setModel(new DefaultTreeModel(new FileSystemTreeNode(file)));
  }


  /**
   * @param args
   */
  public static void main(String[] args) {
    new JTreeDirectoryView();
  }

  class FileSystemTreeNode extends DefaultMutableTreeNode {

    WeakReference<File[]> cachedFilesReference;


    public FileSystemTreeNode(File file) {
      setUserObject(file);
    }


    public int getChildCount() {
      return ((File) getUserObject()).list().length;
    }


    public TreeNode getChildAt(int index) {
      if (null == cachedFilesReference || null == cachedFilesReference.get()) {
        cachedFilesReference = new WeakReference<File[]>(((File) getUserObject()).listFiles());
      }
      File[] files = cachedFilesReference.get();
      return new FileSystemTreeNode(files[index]);
    }


    public boolean isLeaf() {
      return !((File) getUserObject()).isDirectory();
    }


    public String toString() {
      File file = ((File) getUserObject());
      String fileName = file.getName();
      if ("".equals(fileName)) {
        if (FileSystemView.getFileSystemView().isDrive(file)) {
          fileName = file.getPath();

        }
      }
      return fileName;
    }


    public boolean equals(Object obj) {
      return getUserObject().equals(((DefaultMutableTreeNode) obj).getUserObject());
    }


    @Override
    public int hashCode() {
      // TODO Auto-generated method stub
      return getUserObject().hashCode();
    }
  }
}

Gruß Tom
 

Anhänge

  • fileSystemView.jpg
    fileSystemView.jpg
    23,4 KB · Aufrufe: 490
Zurück