简体   繁体   English

JTree-仅具有固定文件扩展名的文件系统查看器树

[英]JTree - file system viewer tree only with fixed file extensions

I'm in the process of writing a school project and I have one problem i can't solve. 我正在编写一个学校项目,但是有一个我无法解决的问题。 I have a tree that shows directories and file paths but I need to display only .mp3 and .wav files. 我有一棵显示目录和文件路径的树,但是我只需要显示.mp3和.wav文件。 Can someone give me some advice how to do that? 有人可以给我一些建议怎么做吗? I have a class with a tree model: 我有一个带有树模型的类:

public boolean isLeaf(Object node) {
    File file = (File) node;
    return file.isFile();
}

public int getIndexOfChild(Object parent, Object child) {
    File directory = (File) parent;
    File file = (File) child;
    String[] children = directory.list();
    for (int i = 0; i < children.length; i++) {
        if (file.getName().equals(children[i])) {
            return i;
        }
    }
    return -1;

}

public void valueForPathChanged(TreePath path, Object value) {
    File oldFile = (File) path.getLastPathComponent();
    String fileParentPath = oldFile.getParent();
    String newFileName = (String) value;
    File targetFile = new File(fileParentPath, newFileName);
    oldFile.renameTo(targetFile);
    File parent = new File(fileParentPath);
    int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) };
    Object[] changedChildren = { targetFile };
    fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);

}

private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) {
    TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
    Iterator iterator = listeners.iterator();
    TreeModelListener listener = null;
    while (iterator.hasNext()) {
        listener = (TreeModelListener) iterator.next();
        listener.treeNodesChanged(event);
    }
}

public void addTreeModelListener(TreeModelListener listener) {
    listeners.add(listener);
}

public void removeTreeModelListener(TreeModelListener listener) {
    listeners.remove(listener);
}

private class TreeFile extends File {
    public TreeFile(File parent, String child) {
        super(parent, child);
    }

    public String toString() {
        return getName();
    }
}

String getFileDetails(File file) {
    if (file == null)
        return "";
    StringBuffer buffer = new StringBuffer();
    buffer.append("Name: " + file.getName() + "\n");
    buffer.append("Path: " + file.getPath() + "\n");
    buffer.append("Size: " + (double) (file.length() / 1024) / 1024 + " MB" + "\n");
    return buffer.toString();
}
String[] children = directory.list();

Wherever you are listing the children, like in the above, know that File.list() can also take a FilenameFilter . 就像上面列出的那样,无论您在哪里列出子代,都知道File.list()也可以使用FilenameFilter There is also a similar method called File.listFiles() which can take a FileFilter (very similar). 还有一个称为File.listFiles()的相似方法,该方法可以使用FileFilter (非常相似)。 Using one of these, you can filter your files by filename (which includes the extension). 使用其中之一,您可以按文件名(包括扩展名)过滤文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM