This section illustrates you how to get the files from the available directories and create a tree.
SWT provides a class Tree to represent the data in the hierarchical way and TreeItem to represents the hierarchy of tree items in a tree by adding an item to the tree.
In the given example, we are creating a tree which represents the hierarchical structure of available files in the directories. The class File performs all the operations of the files and directories. The method listRoots() provides the list of available file system roots. The method listFiles() returns an array of file paths. The method getName() returns the name of the file. In order to find whether the file is directory, we have used the method isDirectory().
The class TreeItem adds the item to the tree by the method setText() and setData(). The method getItems() returns an array of TreeItems. The method getData() returns the data of the particular file.
Here is the code of TreeExample.java
Output will be displayed as
Liked it! Share this Tutorial