JTree文件过滤器和文件夹过滤器 [英] JTree File Filter and Folder Filter

查看:74
本文介绍了JTree文件过滤器和文件夹过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个JTree,它在当前目录中显示所有扩展名为".h"的文件,并且它应该在当前目录中显示除名为"System Volume Information"的文件夹以外的所有文件夹.我的代码,我还需要做些什么..更新?

I want to have a JTree which display's all the files with only ".h" extension in my current directory, and it should display all the folders, except a folder named 'System Volume Information', in my current directory, here's my code, what addition.. updation i need to do ??

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

推荐答案

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

Take advantage of the API. The File class provides the ability to filter the returned results from the list method...

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对象,所以这可能会改变屏幕上的结果,但是可以通过使用

As near as I can tell, this is what your looking for. This may change the result on the screen, as I'm using the File object directly, but it can be fixed by using a 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);
    }
}

这篇关于JTree文件过滤器和文件夹过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆