来自文件路径的 Java JTree 目录结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14831901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:32:47  来源:igfitidea点击:

Java JTree directory structure from file paths

javaswingdirectoryjtreefilepath

提问by ubergam3r

I've been trying to get my head around this so maybe some of you can help me. I have a list of files with their full paths (these are just strings the files are on another machine), e.g:

我一直在努力解决这个问题,所以也许你们中的一些人可以帮助我。我有一个带有完整路径的文件列表(这些只是文件在另一台机器上的字符串),例如:

C:\a\b\c\file1.txt
C:\a\b\c\file2.txt
C:\a\d\file3.txt
C:\e\file4.txt

I want to create a Jtree to show the directory structure like this:

我想创建一个 Jtree 来显示这样的目录结构:

C:
  a
   b
    c
     file1.txt
     file2.txt
   d
    file3.tct
  e
   file4.txt

I've been spliting the string on the seperator so I end up with a list of arrays like:

我一直在分隔符上拆分字符串,所以我最终得到了一个数组列表,如:

"C:","a","b","c","file1.txt"
"C:","a","b","c","file2.txt"
"C:","a","d","file3.txt"
"C:","e","file4.txt"

Now I want to add them an index at a time but if the value already exists at that level then to skip to the next index. i.e it would add the first array then on the second array it would go on level 0 of the tree there already exists a "C:" so move onto level 1 of the tree and index 1 of the array. The issues that I have is that Im not sure how to navigate the tree in such a way.

现在我想一次为它们添加一个索引,但如果该值已存在于该级别,则跳到下一个索引。即它会添加第一个数组,然后在第二个数组上它会进入树的第 0 层,那里已经存在一个“C:”,所以移动到树的第 1 层和数组的索引 1。我遇到的问题是我不确定如何以这种方式导航树。

Any suggestions and or alternative implementations?

任何建议和/或替代实现?

回答by trashgod

Let Filedo the work of parsing and maintaining paths. As you want to display the files in a JTree, you might as well create a corresponding TreeModelsuch as FileTreeModel, cited here. Because it implements TreeModel, it can "be set as a JTree's model and then you'd have a plain old standard JTree." You can use any Filein any mounted file system as the root, for example:

让我们File来做解析和维护路径的工作。既然要在a中显示文件JTree,不妨创建一个对应的TreeModelFileTreeModel,引用here。因为它实现了TreeModel,它可以“被设置为 aJTree的模型,然后你就会有一个普通的旧标准” JTree。您可以File在任何挂载的文件系统中使用 any作为根,例如:

TreeModel model = new FileTreeModel(new File(System.getProperty("user.dir")));
JTree tree = new JTree(model);

image

图片

回答by murison

I'm not sure if FileTreeModel is the best way - it scans entire directories. From what you wrote, I guess you only want to display paths from your list.
You can achieve it by using TreePathsTreeModel described here: How I Show Windows Registry in jTree?
You just have to to convert filepaths from strings into TreePath objects.

我不确定 FileTreeModel 是否是最好的方法 - 它扫描整个目录。从你写的内容来看,我猜你只想显示列表中的路径。
您可以使用此处描述的 TreePathsTreeModel 来实现它:How I Show Windows Registry in jTree?
您只需要将文件路径从字符串转换为 TreePath 对象。

回答by SJuan76

First, sort the Strings (before splitting them).

首先,对字符串进行排序(在拆分它们之前)。

How to process the first line is obvious and I won't comment on it. In the second line, search the already built tree and check if the nodes already exist. After you find one that does not exist, follow the procedure done in the first line.

如何处理第一行很明显,我不会评论。在第二行,搜索已经构建的树并检查节点是否已经存在。找到不存在的一个后,按照第一行中完成的步骤进行操作。