Search code examples
javaswingawtjtree

Creating dynamic JTree from absolute file path


private void createNodes(DefaultMutableTreeNode top) {
    List <String> files = new ArrayList<String>();      
    //getFileNames() returns array list of file name with complete path
    //eg C:/lanceTest1/directory1/sample.txt
    files = getFileNames();     
    DefaultMutableTreeNode node = null;
    for (String fileName : files) {
        node = new DefaultMutableTreeNode(fileName);
        top.add(node);               
    }
}

Above function creates below window.

enter image description here

I want to show it as below (for eg for the first file),

The Java Series
 |__C
  |__lanceTest
   |__directory1
    |__PDxxxx_Splunk_Solution_Architecture_Doc_v0.9.doc

I am new to swing, awt. Can someone help?


Solution

  • Thanks all for your help. Basically I wanted to represent file system from the path which i retrieve from database. Files/directories are not actually stored on database but are dynamically generated from absolute/relative path.

    I found answer here...

    Java Tree to represent filesystem (files/dir) from list of paths