TreeView is one of the most important controls which implements a hierarchical view of data in a tree-like format in GUI-based Java Programming using JavaFX. “Hierarchical” means some items are placed as subordinate items to others, for example, a tree is commonly used for displaying contents of the file system in which the individual files are subordinate to the directory to which they belong. TreeView is a simple conceptual implementation of the java GUI program using a tree data structure.
A tree is represented with a single root node that indicates the start of a tree, under the root node one or more child nodes are attached, child nodes are of two types
- Leaf nodes
- Branch nodes
- Leaf nodes are nodes that have no children also called (Terminal nodes) whereas branch nodes are nodes that form root nodes for sub-trees. A sequence of nodes that leads from root to a specific node is known as a path.
- The most useful feature of TreeView is it automatically provides scrollbars whenever the size of the tree exceeds the dimensions of view.
How to Implement TreeView using JavaFX class:
1. Importing Necessary libraries:
We invoke the necessary libraries to activate JavaFX controls and gather all resources to use TreeView as shown below :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
2. Creating a TreeView:
First, we create a TreeView object by calling a new instance of TreeView class, an example of how to set an object for TreeView class is shown in the below demonstrated example:
TreeView tV = new TreeView();
3. Appending TreeView to a Scene Graph:
Our next task is to add TreeView to JavaFx Scene Graph so that it can be visible, below code snippet is used to enforce this action :
public void start(Stage primaryStage) {
TreeView tV = new TreeView();
VBox vb = new VBox(tV);
Scene s = new Scene(vb);
primaryStage.setScene(s);
primaryStage.show();
}
4. Appending Tree items to TreeView :
The items which are going to be displayed by a JavaFx TreeView are represented by TreeItem Class(javafx.scene.control.TreeItem).
TreeItem rItem = new TreeItem(“Tutorials”);
TreeItem wItem = new TreeItem(“Web Tutorials”);
wItem.getChildren().add(new TreeItem(“HTML Tutorial”));
wItem.getChildren().add(new TreeItem(“HTML5 Tutorial”));
wItem.getChildren().add(new TreeItem(“CSS Tutorial”));
wItem.getChildren().add(new TreeItem(“SVG Tutorial”));
rItem.getChildren().add(webItem);
TreeItem javaItem = new TreeItem(“Java Tutorials”);
javaItem.getChildren().add(new TreeItem(“Java Language”));
javaItem.getChildren().add(new TreeItem(“Java Collections”));
javaItem.getChildren().add(new TreeItem(“Java Concurrency”));
rootItem.getChildren().add(javaItem);
TreeView tV = new TreeView();
tV.setRoot(rItem);
5. Adding Children to TreeView:
In TreeView the Parent-Children relationship between elements runs in a recursive-fashion, i.e TreeItem can have otherTreeItem instances as children, we use the method getChildren() to fetch elements from JavaItem and add it to the TreeView by using the method add() as shown in the below code snippets :
TreeItem javaItem = new TreeItem(“Java Tutorials”);
javaItem.getChildren().add(new TreeItem(“Java Language”));
javaItem.getChildren().add(new TreeItem(“Java Collections”));
javaItem.getChildren().add(new TreeItem(“Java Concurrency”));
TreeItem rItem = new TreeItem(“Tutorials”);
rItem.getChildren().add(javaItem);
6. Hiding RootItem of TreeView :
The last and foremost step is hiding the root item i.e the root node of a JavaFx TreeView. We do so by invoking setShowRoot() method which is initialized with a boolean parameter false.
tV.setShowRoot(false);
Java
// Java Program to implement javaFx based TreeView // Importing all necessary libraries import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.scene.layout.VBox; import javafx.stage.Stage; // Main class // This class is extending Application class public class GFG extends Application { // Main driver method public static void main(String[] args) { launch(args); } // @Override public void start(Stage primaryStage) { // Initializing variable to TreeItem element // All arguments are custom entries TreeItem rItem = new TreeItem( "Tutorials" ); // Initializing variable for TreeItem variable TreeItem wItem = new TreeItem( "Web Tutorials" ); // Adding labels for elements to TreeItem // Custom entries wItem.getChildren().add( new TreeItem( "HTML Tutorial" )); wItem.getChildren().add( new TreeItem( "HTML5 Tutorial" )); wItem.getChildren().add( new TreeItem( "CSS Tutorial" )); wItem.getChildren().add( new TreeItem( "SVG Tutorial" )); rItem.getChildren().add(wItem); // Initializing new TreeItem TreeItem javaItem = new TreeItem( "Java Tutorials" ); javaItem.getChildren().add( new TreeItem( "Java Language" )); javaItem.getChildren().add( new TreeItem( "Java Collections" )); javaItem.getChildren().add( new TreeItem( "Java Concurrency" )); rootItem.getChildren().add(javaItem); // Creating an object of TreeView class TreeView tV = new TreeView(); tV.setRoot(rootItem); tV.setShowRoot( false ); // Creating an object of VBox class VBox vb = new VBox(tV); // Creating an object of Scene class Scene s = new Scene(vb); // Now, setting the scene for primaryStage primaryStage.setScene(s); // Finally, display all the elements // using show() method primaryStage.show(); } } |
Output :
Note: The elements entered in the above image are only to show as an example, you can label the items in TreeView depending upon the context of the situation.