JavaFX Tree Table View

0
  • Tree Table View is a combination of TreeView And TableView.
  • It designed to show the Tree in table form So that we can add more columns.
  • According to oracle documentation

    “The TreeTableView control is designed to visualize an unlimited number of rows of data, broken out into columns.”

Methods used in tree table view:

  • setCellValueFactory()
    The setCellValueFactory methods define TreeItem content for each column.
  • CellDataFeatures()
    CellDataFeatures is used to provide all necessary information for a particular cell.
  • ReadOnlyStringWrapper()
    This class provides a class to define read-only properties And it can be passed to external users.

Example of tree table view:


package javafxtuts;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableColumn.CellDataFeatures;
import javafx.scene.control.TreeTableView;
import javafx.stage.Stage;


/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {
 
    public static final String Column1MapKey = "A";
    public static final String Column2MapKey = "B";
 
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage primaryStage) {
        
        primaryStage.setTitle("JavafxTuts.com");
        //to create a root(tree root)
        TreeItem root = new TreeItem("Subjects");
        //Creating tree items
        TreeItem Computer = new TreeItem("Computer");
        TreeItem Maths = new TreeItem("Maths");
        TreeItem English = new TreeItem("English");
        // to create columns
        TreeTableColumn column = new TreeTableColumn("Class A");
        TreeTableColumn column1 = new TreeTableColumn("Class B");
        column.setPrefWidth(150);
        column1.setPrefWidth(100);
        //The setCellValueFactory methods define TreeItem content for each column
        //CellDataFeatures is used to provide all necessary information for a cell
       column.setCellValueFactory((CellDataFeatures p) -> 
               //first getValue() Returns the value passed in to the constructor
               //getValue() Returns the data represented by this TreeItem
            new ReadOnlyStringWrapper(p.getValue().getValue())); 
       
       //to add children to the root
       root.getChildren().setAll(Computer, Maths, English);
       //to create TreeTableView
       TreeTableView ttv=new TreeTableView(root);
       //to add columns to the ttv
       ttv.getColumns().addAll(column,column1);
       
        //to create a scene using ttv
      Scene scene = new Scene(ttv, 250, 300);
      primaryStage.setScene(scene);
      primaryStage.show();

    }
}

 

Output :

Explanation of tree table view code :

  • TreeItem root = new TreeItem("Subjects");
    It create TreeItem root
  • Create column AS
    TreeTableColumn column = new TreeTableColumn("Class A");
  • Setting prefered width AS
    column.setPrefWidth(150);
  • The setCellValueFactory methods define TreeItem content for each column
    CellDataFeatures is used to provide all necessary information for a cellcolumn.setCellValueFactory(( CellDataFeatures p) ->
    new ReadOnlyStringWrapper( p.getValue().getValue()));

    First getValue() Returns the value passed in to the constructor
    Second getValue() Returns the data represented by this TreeItem

  • Create TreeTableView ttv with root perameter
    TreeTableView ttv=new TreeTableView(root);
  • Adding columns to the TreeTableView(ttv) AS
    ttv.getColumns().addAll(column,column1);
Share.

Leave A Reply