Search code examples
javaswingjtree

How to generate a File Tree in Java Swing directly by providing a absolute path?


My requirement is that there is a browse button on my UI.When I select a directory and press Open,then using the selected path a tree structure should appear.The parent/root directory will start from the folder that I selected.

Eg: If I select c:/mydocuments/myfiles,then the root for the tree will be myfiles and then it should show all its children in the tree.

Presently in swings JTree can be used to generate a tree structure but for doing this we need to make a list and then supply it to the JTree.

In my scenario,it becomes difficult as everytime I need to generate a list the moment path changes.

So i want to know that in java do we have anything like jqueryfiletree which takes the absolute path and generates the tree automatically.


Solution

  • You do not need to make a list. You should just have code that can generate a TreeModel based on a file path.

    And that code is available on a lot of locations on the internet. For example in this Sun JTree article

    Another nice example is the 'File Browser GUI' from Andrew Thompson who is also active on this site