FileTreeModel - TreeModel to display file system directories and files
Whenever your software has to interact with users to display file system structures it would be helpful to do so using a visualization of a directory Tree.
When you use a JTree to display such tree structures you can provide its constructor with an implementation of an interface called TreeModel.
The TreeModel implementation below is running recursively through all sub directories in a provided root directory.
By implementing the method getChildCount to return a count greater than zero, it is indicating that each directory should be recursively searched for files (children) and more directories. Of course the other directories have to implemented accordingly.
I did not come up with this idea by myself, but found it on the internet. However, the versions I found on the internet have not been working properly, so I deceided to code one, that will work.
Enjoy.
Class FileTreeModel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
packageconsulting.bolte.gui;importjava.io.File;importjava.util.Arrays;importjavax.swing.event.TreeModelListener;importjavax.swing.tree.TreeModel;importjavax.swing.tree.TreePath;publicclassFileTreeModelimplementsTreeModel{/*** Creating an object of this class and setting its root to the provided* File object.* * The root is the highest directory available in an object of this class.* * @param file* - an object of type File, giving the root directory for an* object of type FileTreeModel.*/publicFileTreeModel(Filefile){this.root=file;}@OverridepublicObjectgetRoot(){returnthis.root;}@OverridepublicObjectgetChild(Objectparent,intindex){Filef=(File)parent;returnf.listFiles()[index];}@OverridepublicintgetChildCount(Objectparent){Filef=(File)parent;try{if(!f.isDirectory()&&f.list()!=null){return0;}else{returnf.list().length;}}catch(NullPointerExceptionex){return0;}}@OverridepublicbooleanisLeaf(Objectnode){Filef=(File)node;return!f.isDirectory();}@OverridepublicvoidvalueForPathChanged(TreePathpath,ObjectnewValue){// TODO Auto-generated method stub}@OverridepublicintgetIndexOfChild(Objectparent,Objectchild){Filepar=(File)parent;Filech=(File)child;returnArrays.asList(par.listFiles()).indexOf(ch);}@OverridepublicvoidaddTreeModelListener(TreeModelListenerl){// TODO Auto-generated method stub}@OverridepublicvoidremoveTreeModelListener(TreeModelListenerl){// TODO Auto-generated method stub}privateFileroot;}
How to use this implementation of TreeModel
Below a little example code on how the above TreeModel implementation can be used.
1
2
3
4
5
6
7
8
9
10
11
12
13
TheJTreeisputintoaJScrollPane,inordertohaveitscrollable./*** Custom code. Adding a custom implementation of TreeModel called* FileTreeModel in order to display file systems directory structure* automatically.*/TreeModelfileModel=newFileTreeModel(File.listRoots()[0]);getContentPane().setLayout(newGridLayout(0,3,0,0));JScrollPanescrollPane=newJScrollPane();srcDirTree=newJTree(fileModel);scrollPane.setViewportView(srcDirTree);getContentPane().add(scrollPane);