gpt4 book ai didi

java - JTree 不过滤扩展

转载 作者:行者123 更新时间:2023-12-02 01:49:13 25 4
gpt4 key购买 nike

我被这个问题困扰了:

我有一个 JTree,它显示整个目录及其子目录,但我需要对其进行过滤以仅显示具有某些扩展名(例如“.m”、“.py”)的目录和文件。

package classes;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class FileTree implements TreeModel{
private File root;
private Vector listeners = new Vector();

public FileTree(File rootDirectory){
root = rootDirectory;
}

@Override
public Object getRoot(){
return root;
}

@Override
public Object getChild(Object parent, int index){
File directory = (File) parent;
String[] children = directory.list(new FilenameFilter(){
@Override
public boolean accept(File dir, String name){
return name.endsWith(".m") || dir.isDirectory();
}
});
return new FileTree.TreeFile(directory, children[index]);
}

@Override
public int getChildCount(Object parent){
File file = (File) parent;
if (file.isDirectory()){
String[] fileList = file.list();
if (fileList != null){
return file.list().length;
}
}
return 0;
}

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

@Override
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;
}

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

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

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

private class TreeFile extends File{

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

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

在我的 main() 方法中,我这样调用它

JTree tree = new JTree();
tree.setModel(new FileTree(new File("\\C:\\")));
treePanel.setViewportView(tree);

我认为问题出在 getChild() 方法中,但我只能弄清楚如何解决它。

file.list()file.listFiles() 方法之间有什么区别吗?因为我已经用过这两个了,但还是不明白。

编辑:遵循@Thomas建议后,现在它向我显示此错误:

PS:当我在 getChild() 方法上打印“children”变量以进行调试时,它会返回“C:\”中的所有目录(共有 14 个)。

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 14

更新的方法:

    @Override
public int getIndexOfChild(Object parent, Object child){
File directory = (File) parent;
File file = (File) child;
String[] children = directory.list(new FilenameFilter(){
@Override
public boolean accept(File dir, String name){
return name.endsWith(".m") || new File(dir, name).isDirectory();
}
});
for (int i = 0; i < children.length; i++){
if (file.getName().equals(children[i])){
return i;
}
}
return -1;
}

    @Override
public int getChildCount(Object parent){
File file = (File) parent;
if (file.isDirectory()){
String[] fileList = file.list(new FilenameFilter(){
@Override
public boolean accept(File dir, String name){
return name.endsWith(".m") || new File(dir, name).isDirectory();
}
});
if (fileList != null){
return file.list().length;
}
}
return 0;
}

最佳答案

问题在于您对 FilenameFilter 的实现。

FilenameFilter#accept ( https://docs.oracle.com/javase/8/docs/api/java/io/FilenameFilter.html#accept-java.io.File-java.lang.String- ) 方法的 javadoc 描述了参数:

  • dir - 找到文件的目录。
  • 名称 - 文件的名称。

从这个描述中可以清楚地看出您的接受方法:

    public boolean accept(File dir, String name){
return name.endsWith(".m") || dir.isDirectory();
}

将始终返回true,因为 dir 始终是一个目录。

如果你想检查目录 dir 中的条目 name 是否是一个目录,你必须将你的接受方法重写为

    public boolean accept(File dir, String name){
return name.endsWith(".m") || new File(dir, name).isDirectory();
}

这只会为您提供名称以“.m”结尾的目录和文件。

但现在您还需要更改 getChildCount()getIndexOfChild() 方法 - 因为目前它们基于未过滤的子列表。

关于java - JTree 不过滤扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53239361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com