Java JTree directory structure from file paths
Solution 1:
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);
Solution 2:
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.