Monday 18 June 2018

TreeView: Working with Tree Cell Editors

JavaFX provides below cell editors.
a.   CheckBoxTreeCell,
b.   ChoiceBoxTreeCell,
c.    ComboBoxTreeCell,
d.   TextFieldTreeCell

By using cell editors, we can render control in the cell. In this post, I am going to show you how to render checkboxes in tree item.

Step 1: Create TreeView and set the cell factory to CheckBoxTreeCell.

TreeView<String> treeView = new TreeView<>(rootItem);
treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView());

Step 2: Create TreeItem instances using ‘CheckBoxTreeItem’.

CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>("College");
TreeItem<String> physicsDepartment = new CheckBoxTreeItem<>("Physics Department");
TreeItem<String> chemistryDepartment = new CheckBoxTreeItem<>("Chemistry Department");
TreeItem<String> mathsDepartment = new CheckBoxTreeItem<>("Maths Department");

Find the below working application.

TreeViewApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TreeViewApp extends Application {

 @SuppressWarnings("unchecked")
 @Override
 public void start(Stage primaryStage) throws Exception {
  /* Define Root Item */
  CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>("College");
  rootItem.setExpanded(true);

  /* Defile 1st level child items */
  TreeItem<String> physicsDepartment = new CheckBoxTreeItem<>("Physics Department");
  TreeItem<String> chemistryDepartment = new CheckBoxTreeItem<>("Chemistry Department");
  TreeItem<String> mathsDepartment = new CheckBoxTreeItem<>("Maths Department");

  /* Add items to root item */
  rootItem.getChildren().addAll(physicsDepartment, chemistryDepartment, mathsDepartment);

  /* Adding Physics faculty details to physics department */
  TreeItem<String> physicsFaculty1 = new CheckBoxTreeItem<>("Srinivasa Rao");
  TreeItem<String> physicsFaculty2 = new CheckBoxTreeItem<>("Madavi Latha");
  physicsDepartment.getChildren().addAll(physicsFaculty1, physicsFaculty2);
  physicsDepartment.setExpanded(true);

  /* Adding chemistry faculty details to physics department */
  TreeItem<String> chemistryFaculty1 = new CheckBoxTreeItem<>("Naga Veni");
  TreeItem<String> chemistryFaculty2 = new CheckBoxTreeItem<>("Raghava Rao");
  chemistryDepartment.getChildren().addAll(chemistryFaculty1, chemistryFaculty2);
  chemistryDepartment.setExpanded(true);

  /* Adding maths faculty details to maths department */
  TreeItem<String> mathsFaculty1 = new CheckBoxTreeItem<>("Venkata Anjaneyulu");
  TreeItem<String> mathsFaculty2 = new CheckBoxTreeItem<>("Rama Krishna");
  mathsDepartment.getChildren().addAll(mathsFaculty1, mathsFaculty2);
  mathsDepartment.setExpanded(true);

  TreeView<String> treeView = new TreeView<>(rootItem);
  treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView());   

  StackPane stackPane = new StackPane();
  stackPane.getChildren().add(treeView);

  primaryStage.setScene(new Scene(stackPane));
  primaryStage.setTitle("Tree View Example");
  primaryStage.setWidth(900);
  primaryStage.setHeight(500);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(TreeViewApp.class, args);
 }
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment