Java JTree directory structure from file paths

与世无争的帅哥 提交于 2019-11-26 16:44:14
trashgod

Let File do the work of parsing and maintaining paths. As you want to display the files in a JTree, you might as well create a corresponding TreeModel such 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 File in any mounted file system as the root, for example:

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!