Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to use the TreeView in JavaFX | 100% Perfect Tutorial

July 29, 2023
in Java
Reading Time: 4 mins read
0
TreeView in JavaFX
391
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • How to use the TreeView in JavaFX
    • Create the TreeView
    • Displaying TreeView
  • Example 1: Put the code all together
    • Output
  • Example 2: Advance JavaFX TreeView
    • Output
  • YouTube Video

How to use the TreeView in JavaFX

What’s up! In this tutorial, you will learn how to use the TreeView in JavaFX. The TreeView is a useful and powerful control to use in JavaFX. This control allows us and the users to display hierarchical data. JavaFX TreeView has collapsible and expandable features which will be a good feature to help the user interface to be organized.

When you would like to expand the TreeView, you will first need to expand the parent or the root node, child or branch node and last the last data will be the leaf node. When all TreeItems are expanded, it provides a visual representation of a tree structure. If you are not familiar with the TreeItem, JavaFX TreeItem is used to create the nodes like the root or parent node until the leaf node.

Create the TreeView

Let’s get started and learn more about the TreeView and let’s start by creating the JavaFX TreeView. In this example, I will provide you with the code to create the TreeView, the provided code snippet below will guide you in creating it. You’ll need to use the TreeView class and we’ll use String as its generic type as shown in the code below.

TreeView<String> treeView = new TreeView<>();

Now, we’ve created the TreeView in JavaFX but we’re not done yet. The TreeView is also a parameterized constructor, just like the other nodes. You can also assign a node or the root node there. You can set the root node as well like treeView.setRoot(treeItem); We also need to create the TreeItem. A TreeView without a TreeItem is useless, so we must create this node. TreeItem is a piece of data in the tree which represents the parent, branch or the leaf node. Now, please see the code as shown below to see how it works.

TreeItem<String> root = new TreeItem<>("Root");

TreeItem<String> branch1 = new TreeItem<>("Branch 1");
TreeItem<String> branch2 = new TreeItem<>("Branch 2");

TreeItem<String> leaf1 = new TreeItem<>("leaf 1");
TreeItem<String> leaf2 = new TreeItem<>("leaf 2");
TreeItem<String> leaf3 = new TreeItem<>("leaf 3");
TreeItem<String> leaf4 = new TreeItem<>("leaf 4");

root.getChildren().addAll(branch1, branch2);
branch1.getChildren().addAll(leaf1, leaf2);
branch2.getChildren().addlAll(leaf3, leaf4);

Displaying TreeView

In this part of this basic tutorial, you will learn how to display the TreeView in the application window or the JavaFX Stage. You also need to learn more about layout in JavaFX to create the layout for the scene. If you already know how to display the nodes to the stage, this will be easy for you. The following code as shown below will show you how to display the TreeView in JavaFX with a scene on the Stage.

VBox layout = new VBox(treeView);
Scene scene = new Scene(layout, 400, 400);
stage.setScene(scene);
stage.setTitle("JavaFX TreeView");
stage.show();

Example 1: Put the code all together

In this example, we will put the codes together as we’ve learned the basic example of creating the TreeView in JavaFX.

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;

import java.io.IOException;

public class TreeViewMain extends Application {
    @Override
    public void start(Stage stage) throws IOException {

        TreeItem<String> root = new TreeItem<>("Root");

        TreeView<String> treeView = new TreeView<>(root);
        treeView.setShowRoot(true);

        TreeItem<String> branch1 = new TreeItem<>("Branch 1");
        TreeItem<String> branch2 = new TreeItem<>("Branch 2");

        TreeItem<String> leaf1 = new TreeItem<>("leaf 1");
        TreeItem<String> leaf2 = new TreeItem<>("leaf 2");
        TreeItem<String> leaf3 = new TreeItem<>("leaf 3");
        TreeItem<String> leaf4 = new TreeItem<>("leaf 4");

        root.getChildren().addAll(branch1, branch2);
        branch1.getChildren().addAll(leaf1, leaf2);
        branch2.getChildren().addAll(leaf3, leaf4);

        VBox layout = new VBox(treeView);
        Scene scene = new Scene(layout, 400, 400);
        stage.setScene(scene);
        stage.setTitle("JavaFX TreeView");
        stage.show();

    }

    public static void main(String[] args) {
        launch();
    }
}

Output

TreeView in JavaFX

Example 2: Advance JavaFX TreeView

In this example, I will provide an advanced example code for creating the JavaFX TreeView. The code will execute dynamically from a selected path on your computer and it will automatically generate TreeItems based on the files from the selected path. To learn more about it, please proceed to the given code 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;

import java.io.File;
import java.io.IOException;

public class TreeViewMain extends Application {
    @Override
    public void start(Stage stage) throws IOException {

        File filePath = new File("D:\\IDEA Projects"); // you can change the path here to your desired path
        
        // Create the root and items
        TreeItem<String> root = createItems(filePath);

        // create the treeview
        TreeView<String> treeView = new TreeView<>(root);

        // Create the Scene and show the stage
        VBox layout = new VBox(treeView);
        Scene scene = new Scene(layout, 400, 400);
        stage.setScene(scene);
        stage.setTitle("JavaFX TreeView");
        stage.show();

        // This code will print out the selected items on the TreeView
        treeView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue ) ->{
            String selectedItem = newValue.getValue();
            System.out.println(selectedItem);
        });
    }

    // create the treeitems dynamically based on the given path
    private TreeItem<String> createItems(File file){
        TreeItem<String> treeItems = new TreeItem<>(file.getName());
        if(file.isDirectory()){
            File[] files = file.listFiles();
            if(files != null){
                for(File childItem : files){
                    treeItems.getChildren().addAll(createItems(childItem));
                }
            }
        }

        return treeItems;
    }

    public static void main(String[] args) {
        launch();
    }
}

Output

JavaFX TreeView

YouTube Video

YouTube video
Previous Post

How to use the Table View in JavaFX | 100% Perfect Tutorial

Next Post

How to Play Video in JavaFX MediaPlayer from a File

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
216
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
590
Next Post
How to Play Video in JavaFX MediaPlayer from a File

How to Play Video in JavaFX MediaPlayer from a File

JavaFX Music Player

JavaFX Music Player Tutorial with JavaFX Media Player

JavaFX Drag and Drop

JavaFX Drag and Drop Tutorial | Perfect For Beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.