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
importjava.io.File;importjavafx.collections.FXCollections;importjavafx.collections.ObservableList;importjavafx.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.*/publicclassSimpleFileTreeItemextendsTreeItem<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.*/publicSimpleFileTreeItem(Filef){super(f);}/** (non-Javadoc)* * @see javafx.scene.control.TreeItem#getChildren()*/@OverridepublicObservableList<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));}returnsuper.getChildren();}/** (non-Javadoc)* * @see javafx.scene.control.TreeItem#isLeaf()*/@OverridepublicbooleanisLeaf(){if(isFirstTimeLeaf){isFirstTimeLeaf=false;Filef=(File)getValue();isLeaf=f.isFile();}returnisLeaf;}/*** 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.*/privateObservableList<TreeItem<File>>buildChildren(TreeItem<File>TreeItem){Filef=TreeItem.getValue();if(f!=null&&f.isDirectory()){File[]files=f.listFiles();if(files!=null){ObservableList<TreeItem<File>>children=FXCollections.observableArrayList();for(FilechildFile:files){children.add(newSimpleFileTreeItem(childFile));}returnchildren;}}returnFXCollections.emptyObservableList();}privatebooleanisFirstTimeChildren=true;privatebooleanisFirstTimeLeaf=true;privatebooleanisLeaf;}
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
importjava.io.File;importjavafx.application.Application;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.control.Menu;importjavafx.scene.control.MenuBar;importjavafx.scene.control.MenuItem;importjavafx.scene.control.SplitPane;importjavafx.scene.control.ToolBar;importjavafx.scene.control.TreeView;importjavafx.scene.layout.BorderPane;importjavafx.scene.layout.VBox;importjavafx.scene.web.HTMLEditor;importjavafx.stage.Stage;publicclassMainextendsApplication{@Overridepublicvoidstart(StageprimaryStage){try{/* Create a new MenuBar. */MenuBarmenu=newMenuBar();/* Create new sub menus. */MenumenuFile=newMenu("File");MenumenuHelp=newMenu("Help");MenuItemabout=newMenuItem("About");about.setOnAction(newEventHandler<ActionEvent>(){@Overridepublicvoidhandle(ActionEventevent){/** 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. */Buttonbtn=newButton();btn.setText("Say Hello World.");btn.setOnAction(newEventHandler<ActionEvent>(){@Overridepublicvoidhandle(ActionEventevent){System.out.println("Hello World!");}});/* Create a new ToolBar. */ToolBartools=newToolBar(btn);/* Create a new VBox and add the menu as well as the tools. */VBoxmenus=newVBox();menus.getChildren().addAll(menu,tools);/** Adding a TreeView to the very left of the application window.*/TreeView<File>fileView=newTreeView<File>(newSimpleFileTreeItem(newFile("C:\\")));/* Creating a SplitPane and adding the fileView. */SplitPanesplitView=newSplitPane();splitView.getItems().add(fileView);splitView.getItems().add(newHTMLEditor());/* Create a root node as BorderPane. */BorderPaneroot=newBorderPane();/* 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.*/Scenescene=newScene(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(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){launch(args);}}