JTree文件过滤器和文件夹过滤器

时间:2013-05-27 11:17:39

标签: java swing jtree

我想要一个JTree,它显示当前目录中只有“.h”扩展名的所有文件,并且它应显示当前目录中除名为“System Volume Information”的文件夹之外的所有文件夹,这里是我的代码,还有什么需要做的更新?

import java.awt.BorderLayout;
import java.awt.Container;
import java.io.File;
import java.util.Collections;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class DriveTree extends JPanel
{

    public DriveTree(File dir) 
    {
        setLayout(new BorderLayout());
        JTree tree = new JTree(addNodes(null, dir));
        add(tree);
    }

    DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir)
    {
        String curPath = dir.getPath();
        DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
        if (curTop != null)
        {
          curTop.add(curDir);
        }
        Vector ol = new Vector();
        String[] tmp = dir.list();
        for (int i = 0; i < tmp.length; i++)
        ol.addElement(tmp[i]);
        Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
        File f;
        Vector files = new Vector();
        for (int i = 0; i < ol.size(); i++)
        {
            String thisObject = (String) ol.elementAt(i);
            String newPath;
            if (curPath.equals("."))
            newPath = thisObject;
            else
            newPath = curPath + File.separator + thisObject;
            if ((f = new File(newPath)).isDirectory())
            addNodes(curDir, f);
            else
            files.addElement(thisObject);
        }
       for (int fnum = 0; fnum < files.size(); fnum++)
       curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
       return curDir;
    }

  public static void main(String[] av)
  {

    JFrame frame = new JFrame("Drive View");
    Container cp = frame.getContentPane();
    if (av.length == 0) 
    {
      cp.add(new DriveTree(new File(".")));
    }
    else
    {
      cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
      for (int i = 0; i < av.length; i++)
      cp.add(new DriveTree(new File(av[i])));
    }
    frame.pack();
    frame.setVisible(true);
    frame.setSize(300,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

3 个答案:

答案 0 :(得分:1)

利用API。 File类提供了从list方法过滤返回结果的功能...

File[] file = dir.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        String name = pathname.getName().toLowerCase();
        return name.endsWith(".h") || (pathname.isDirectory() && !("System Volume Information".equalsIgnoreCase(name)));
    }
});

更新了示例

尽我所知,这就是你要找的东西。这可能会更改屏幕上的结果,因为我直接使用File对象,但可以使用TreeCellRenderer

修复此问题
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class DriveTree extends JPanel {

    public DriveTree(File dir) {
        setLayout(new BorderLayout());
        JTree tree = new JTree(addNodes(null, dir));
        add(tree);
    }

    DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
        String curPath = dir.getPath();
        DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
        if (curTop != null) {
            curTop.add(curDir);
        }

        List<File> files = new ArrayList<File>(Arrays.asList(dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                String name = pathname.getName().toLowerCase();
                return name.endsWith(".h") || (pathname.isDirectory() && !("System Volume Information".equalsIgnoreCase(name)));
            }
        })));

        Collections.sort(files);
//        File f;
//        Vector files = new Vector();
//        for (int i = 0; i < ol.size(); i++) {
//            String thisObject = (String) ol.elementAt(i);
//            String newPath;
//            if (curPath.equals(".")) {
//                newPath = thisObject;
//            } else {
//                newPath = curPath + File.separator + thisObject;
//            }
//            if ((f = new File(newPath)).isDirectory()) {
//                addNodes(curDir, f);
//            } else {
//                files.addElement(thisObject);
//            }
//        }
        for (File file : files) {
            if (file.isDirectory()) {
                addNodes(curDir, file);
            }
        }
        for (File file : files) {
            if (file.isFile()) {
                curDir.add(new DefaultMutableTreeNode(file));
            }
        }
        return curDir;
    }

    public static void main(String[] av) {

        JFrame frame = new JFrame("Drive View");
        Container cp = frame.getContentPane();
        if (av.length == 0) {
            cp.add(new DriveTree(new File(".")));
        } else {
            cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
            for (int i = 0; i < av.length; i++) {
                cp.add(new DriveTree(new File(av[i])));
            }
        }
        frame.pack();
        frame.setVisible(true);
        frame.setSize(300, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

答案 1 :(得分:0)

您可以获取要添加的文件的扩展名,并对此扩展程序进行检查:

String extension = "";

int i = fileName.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
    extension = fileName.substring(i+1);
}

if(extension.equals("h"))
{
   //Add to your tree
}

答案 2 :(得分:0)

您可以使用FileNameExtensionFilter

FileNameExtensionFilter filter = new FileNameExtensionFilter(null, "h");

for(//...
    if ((f = new File(newPath)).isDirectory())
        addNodes(curDir, f);
    else if(filter.accept(new File(newPath))) //accept only returns true if the extension matches
        files.addElement(thisObject);