Display file(s) name and select it under the folder in JTree

前端 未结 1 449

I managed to come this far using stackoverflow examples, JTree displays all the system drives and folders, wanted to display all the corresponding files fro

相关标签:
1条回答
  • 2020-12-21 23:21

    Don't add all the file names in a loop. Instead, create a FileTreeModel that implements TreeModel, as shown here. The implementation simply invokes the File method listFiles() in getChild() and getIndexOfChild(). Then you can create a tree and expand any desired row; use setSelectionPath() as shown here.

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

    image

    I get only the c:\; please give me directions to get all the system drives, etc.

    You can get a list of filesystem roots with File.listRoots(), as shown in Find all drive letters in Java, or FileSystemView#getRoots(), as shown in File Browser GUI.

    0 讨论(0)
提交回复
热议问题