What is CheckBoxTreeItem in JavaFX explain with an example?


Check box

A checkbox is a type of selection control, which is square in shape with a tick mark int it, It has three states checked or, unchecked and, tristate/indeterminate (optional). Unlike radio buttons, you cannot combine checkboxes using Toggle Button.

Tree view

A tree provides a view of hierarchical structures, each tree contains a root (highest object) and it contains children. You can create a tree view by instantiating the javafx.scene.control.TreeView class.

A CheckBoxTreeItem is a tree view formed with checkboxes. You can create a checkbox tree item by instantiating the javafx.scene.control.CheckBoxTreeItem class.

Example

The following Example demonstrates the creation of a CheckBoxTreeItem.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.Label;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class CheckBoxTreeExample extends Application {
   public void start(Stage stage) {
      //Creating the check boxes
      CheckBoxTreeItem<String> checkBox1 = new CheckBoxTreeItem<>("English");
      CheckBoxTreeItem<String> checkBox2 = new CheckBoxTreeItem<>("Hindi");
      CheckBoxTreeItem<String> checkBox3 = new CheckBoxTreeItem<>("Telugu");
      CheckBoxTreeItem<String> checkBox4 = new CheckBoxTreeItem<>("Tamil");
      //Creating the label
      Label label = new Label("Select known languages:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Creating the parent check box item
      CheckBoxTreeItem<String> languages = new CheckBoxTreeItem<>("Languages");
      languages.setExpanded(true);
      //Adding all the child check box items it the parent
      languages.getChildren().addAll(checkBox1, checkBox2, checkBox3, checkBox4);
      //Creating a tree view
      TreeView<String> treeView = new TreeView<>();
      //Adding the parent check box item to the tree view
      treeView.setRoot(languages);
      //Setting the cell factory
      treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
      //Setting the height of the tree view
      treeView.setMaxHeight(140);
      //Setting layout
      VBox vBox = new VBox(5);
      vBox.setPadding(new Insets(25, 5, 5, 50));
      vBox.getChildren().addAll(label, treeView);
      //Setting the stage
      Scene scene = new Scene(new Group(vBox), 595, 200, Color.BEIGE);
      stage.setTitle("Check Tree Item");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 19-May-2020

544 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements