Monday 18 June 2018

JavaFX: TreeView: Edit Tree

Step 1: Set the tree view editable status to true.
TreeView<String> treeView = new TreeView<>(rootItem);
treeView.setEditable(true);

Step 2: Create a custom tree cell class by extending ‘TreeCell’ class. Override below methods of TreeCell class.

a.   startEdit
b.   cancelEdit
c.    updateItem

Step 3: Set the custom cell factory.
treeView.setCellFactory((TreeView<String> p) -> new TreeCellTextField());

Find the below working application.

TreeCellTextField.java
package com.sample.demos;

import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class TreeCellTextField extends TreeCell<String> {
 private TextField textField;

 /**
  * On editing, create new text field and set it using setGraphic method.
  */
 @Override
 public void startEdit() {
  super.startEdit();

  if (textField == null) {
   createTextField();
  }
  setText(null);
  setGraphic(textField);
  textField.selectAll();
 }

 /**
  * On cancel edit, set the original content back
  */
 @Override
 public void cancelEdit() {
  super.cancelEdit();
  setText((String) getItem());
  setGraphic(getTreeItem().getGraphic());
 }

 @Override
 public void updateItem(String item, boolean empty) {
  super.updateItem(item, empty);

  if (empty) {
   setText(null);
   setGraphic(null);
   return;
  }

  if (!isEditing()) {
   setText(getString());
   setGraphic(getTreeItem().getGraphic());
   return;
  }

  if (textField != null) {
   textField.setText(getString());
  }
  setText(null);
  setGraphic(textField);

 }

 private void createTextField() {
  textField = new TextField(getString());
  textField.setOnKeyReleased((KeyEvent t) -> {
   if (t.getCode() == KeyCode.ENTER) {
    commitEdit(textField.getText());
   } else if (t.getCode() == KeyCode.ESCAPE) {
    cancelEdit();
   }
  });
 }

 private String getString() {
  return getItem() == null ? "" : getItem().toString();
 }
}

TreeViewApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
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 */
  TreeItem<String> rootItem = new TreeItem<>("College");
  rootItem.setExpanded(true);

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

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

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

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

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

  TreeView<String> treeView = new TreeView<>(rootItem);
  treeView.setEditable(true);
  treeView.setCellFactory((TreeView<String> p) -> new TreeCellTextField());

  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

2 comments: