This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a tree for a file chooser?  RSS feed

 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I go about making a JTree component that I can use as a filechooser? EG, something that shows the directories as a tree structure, and being able to tell what node is currently selected.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's already a JFileChooser component to do that... but if you really wanted to do it yourself you could always just modify DefaultTreeModel to use the listRoots() method of the File class and build a subclass of DefaultMutableTreeNode that wraps a File reference. I would also only read one level of the directory at a time. You could do this by using a TreeExpansionListner and use the listFiles() method to create the children of the directory that's expanding.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!