• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a File Explorer using JTree

 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
How can i create a File Explorer using JTree in Swing so there is a tree representation for the localdisks? Left side showing the tree and right side showing the files of the selected folder in the tree. Expected behaviour is replication of winodws explorer behaviour.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make a JTree for the left and a JTextArea for the right component, both in
JScrollPanes.
A JFileChooser allows you to navigate the file system and select files and directories. If a
directory is selected you can show its files in your tree. Use methods of the File class api
to collect the files. You could make up a small data store class to store the URL for each
file and return the file name in its toString method (which will be called by the treeNode
for display). Use this class as a wrapper for each file and use it as the userObject in each
DefaultMutableTreeNode for the tree.

When the user selects a node you can retrieve the wrapper from the node and get the file URL
which you then use to read the file into the textArea.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic