In order to display a TreeView representing a file sytsem as shown below, I had to extend the TreeItem<T> class of JavaFX.

Just for fun I added an HTML Editor as well and separated both controls using a SplitPane.

SimpleFileTreeItem

Below you can find the source code for a class called SimpleFileTreeItem.

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.io.File;
 
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;
 
/**
 * @author Alexander Bolte - Bolte Consulting (2010 - 2014).
 
 *         This class shall be a simple implementation of a TreeItem for
 *         displaying a file system tree.
 
 *         The idea for this class is taken from the Oracle API docs found at
 *         http
 *         ://docs.oracle.com/javafx/2/api/javafx/scene/control/TreeItem.html.
 
 *         Basically the file sytsem will only be inspected once. If it changes
 *         during runtime the whole tree would have to be rebuild. Event
 *         handling is not provided in this implementation.
 */
public class SimpleFileTreeItem extends TreeItem<File> {
 
	/**
	 * Calling the constructor of super class in oder to create a new
	 * TreeItem<File>.
	 
	 * @param f
	 *            an object of type File from which a tree should be build or
	 *            which children should be gotten.
	 */
	public SimpleFileTreeItem(File f) {
		super(f);
	}
 
	/*
	 * (non-Javadoc)
	 
	 * @see javafx.scene.control.TreeItem#getChildren()
	 */
	@Override
	public ObservableList<TreeItem<File>> getChildren() {
		if (isFirstTimeChildren) {
			isFirstTimeChildren = false;
 
			/*
			 * First getChildren() call, so we actually go off and determine the
			 * children of the File contained in this TreeItem.
			 */
			super.getChildren().setAll(buildChildren(this));
		}
		return super.getChildren();
	}
 
	/*
	 * (non-Javadoc)
	 
	 * @see javafx.scene.control.TreeItem#isLeaf()
	 */
	@Override
	public boolean isLeaf() {
		if (isFirstTimeLeaf) {
			isFirstTimeLeaf = false;
			File f = (File) getValue();
			isLeaf = f.isFile();
		}
 
		return isLeaf;
	}
 
	/**
	 * Returning a collection of type ObservableList containing TreeItems, which
	 * represent all children available in handed TreeItem.
	 
	 * @param TreeItem
	 *            the root node from which children a collection of TreeItem
	 *            should be created.
	 * @return an ObservableList<TreeItem<File>> containing TreeItems, which
	 *         represent all children available in handed TreeItem. If the
	 *         handed TreeItem is a leaf, an empty list is returned.
	 */
	private ObservableList<TreeItem<File>> buildChildren(TreeItem<File> TreeItem) {
		File f = TreeItem.getValue();
		if (f != null && f.isDirectory()) {
			File[] files = f.listFiles();
			if (files != null) {
				ObservableList<TreeItem<File>> children = FXCollections
						.observableArrayList();
 
				for (File childFile : files) {
					children.add(new SimpleFileTreeItem(childFile));
				}
 
				return children;
			}
		}
 
		return FXCollections.emptyObservableList();
	}
 
	private boolean isFirstTimeChildren = true;
	private boolean isFirstTimeLeaf = true;
	private boolean isLeaf;
}

Usage 

How the above implementation can be used is shown in below source code printed in bold characters.

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.File;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitPane;
import javafx.scene.control.ToolBar;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
 
public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		try {
			/* Create a new MenuBar. */
			MenuBar menu = new MenuBar();
			/* Create new sub menus. */
			Menu menuFile = new Menu("File");
			Menu menuHelp = new Menu("Help");
			MenuItem about = new MenuItem("About");
			about.setOnAction(new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent event) {
					/*
					 * Implement dialog to be prompted when users asks for
					 * details.
					 */
				}
			});
			menuHelp.getItems().add(about);
 
			/* Adding all sub menus at ones to a MenuBar. */
			menu.getMenus().addAll(menuFile, menuHelp);
 
			/* Create a button. */
			Button btn = new Button();
			btn.setText("Say Hello World.");
			btn.setOnAction(new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent event) {
					System.out.println("Hello World!");
				}
			});
 
			/* Create a new ToolBar. */
			ToolBar tools = new ToolBar(btn);
 
			/* Create a new VBox and add the menu as well as the tools. */
			VBox menus = new VBox();
			menus.getChildren().addAll(menu, tools);
 
			/*
			 * Adding a TreeView to the very left of the application window.
			 */
			TreeView<File> fileView = new TreeView<File>(
					new SimpleFileTreeItem(new File("C:\\")));
 
			/* Creating a SplitPane and adding the fileView. */
			SplitPane splitView = new SplitPane();
			splitView.getItems().add(fileView);
			splitView.getItems().add(new HTMLEditor());
 
			/* Create a root node as BorderPane. */
			BorderPane root = new BorderPane();
 
			/* Adding the menus as well as the content pane to the root node. */
			root.setTop(menus);
			root.setCenter(splitView);
 
			/*
			 * Setting the root node of a scene as well as the applications CSS
			 * file. CSS file has to be in same src directory as this class. The
			 * path is always relative.
			 */
			Scene scene = new Scene(root, 800, 600);
			scene.getStylesheets().add(
					getClass().getResource("application.css").toExternalForm());
 
			/* Adding a scene to the stage. */
			primaryStage.setScene(scene);
			primaryStage.setTitle("FileRex");
 
			/* Lift the curtain :0). */
			primaryStage.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	public static void main(String[] args) {
		launch(args);
	}
}