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
package consulting.bolte.gui;
 
import java.io.File;
import java.util.Arrays;
 
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
 
public class FileTreeModel implements TreeModel {
 
	/**
	 * 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.
	 */
	public FileTreeModel(File file) {
		this.root = file;
	}
 
	@Override
	public Object getRoot() {
		return this.root;
	}
 
	@Override
	public Object getChild(Object parent, int index) {
		File f = (File) parent;
		return f.listFiles()[index];
	}
 
	@Override
	public int getChildCount(Object parent) {
		File f = (File) parent;
 
		try {
			if (!f.isDirectory() && f.list() != null) {
				return 0;
			} else {
				return f.list().length;
			}
		} catch (NullPointerException ex) {
			return 0;
		}
	}
 
	@Override
	public boolean isLeaf(Object node) {
		File f = (File) node;
		return !f.isDirectory();
	}
 
	@Override
	public void valueForPathChanged(TreePath path, Object newValue) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public int getIndexOfChild(Object parent, Object child) {
		File par = (File) parent;
		File ch = (File) child;
		return Arrays.asList(par.listFiles()).indexOf(ch);
	}
 
	@Override
	public void addTreeModelListener(TreeModelListener l) {
		// TODO Auto-generated method stub
	}
 
	@Override
	public void removeTreeModelListener(TreeModelListener l) {
		// TODO Auto-generated method stub
	}
 
	private File root;
}

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
The JTree is put into a JScrollPane, in order to have it scrollable.
/**
 * Custom code. Adding a custom implementation of TreeModel called
 * FileTreeModel in order to display file systems directory structure
 * automatically.
 */
TreeModel fileModel = new FileTreeModel(File.listRoots()[0]);
getContentPane().setLayout(new GridLayout(0, 3, 0, 0));
 
JScrollPane scrollPane = new JScrollPane();
srcDirTree = new JTree(fileModel);
scrollPane.setViewportView(srcDirTree);
getContentPane().add(scrollPane);