Java JTree directory structure from file paths

I'm trying to get around this, so some of you can help me. I have a list of files with their full paths (these are just lines that are on another computer), for example:

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 Jtree to show the directory structure as follows:

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

I split the line on the separator, so I ended 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 an index to them at a time, but if the value already exists at that level, then go to the next index. that is, he will add the first array, and then to the second array, which will go at level 0 of the tree, "C:" already exists, so go to level 1 of the tree and index 1 of the array. The problems that I have is that I am not sure how to navigate the tree this way.

/ ?

+3
3

File . JTree, TreeModel, FileTreeModel, . TreeModel, " JTree, JTree". File , :

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

image

+6

, FileTreeModel - - . , , , .
, TreePathsTreeModel: Windows jTree? TreePath.

+1

First sort the lines (before splitting them).

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

0
source

All Articles