我陷入了这个问题:
我有一个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个)。
线程“ 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;
}
答案 0 :(得分:2)
问题是您对FilenameFilter
的实现。
FilenameFilter#accept
(https://docs.oracle.com/javase/8/docs/api/java/io/FilenameFilter.html#accept-java.io.File-java.lang.String-)方法的Javadoc描述了参数:
根据此说明,很明显您的accept方法:
public boolean accept(File dir, String name){
return name.endsWith(".m") || dir.isDirectory();
}
将始终返回true
,因为dir始终是目录。
如果要检查目录name
中的条目dir
是否为目录,则必须将accept方法重写为
public boolean accept(File dir, String name){
return name.endsWith(".m") || new File(dir, name).isDirectory();
}
这只会为您提供名称以“ .m”结尾的目录和文件。
但是现在您还需要更改getChildCount()
和getIndexOfChild()
方法-因为当前它们基于未过滤的子列表。