Java TreeItem.isLeaf()
Syntax
TreeItem.isLeaf() has the following syntax.
public boolean isLeaf()
Example
In the following code shows how to use TreeItem.isLeaf() method.
// w w w . ja v a2 s .c o m
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
List<Employee> employees = Arrays.<Employee>asList(
new Employee("a1", "A"),
new Employee("a2", "A"),
new Employee("a3", "A"),
new Employee("b1", "B"),
new Employee("b2", "B"),
new Employee("b3", "B"),
new Employee("c1", "C"),
new Employee("c2", "C"),
new Employee("c3", "C"),
new Employee("c4", "C"),
new Employee("e1", "E"));
TreeItem<String> rootNode = new TreeItem<String>("Root");
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
rootNode.setExpanded(true);
System.out.println(rootNode.isLeaf());
for (Employee employee : employees) {
TreeItem<String> empLeaf = new TreeItem<String>(employee.getName());
boolean found = false;
for (TreeItem<String> depNode : rootNode.getChildren()) {
if (depNode.getValue().contentEquals(employee.getDepartment())){
depNode.getChildren().add(empLeaf);
found = true;
break;
}
}
if (!found) {
TreeItem depNode = new TreeItem(employee.getDepartment());
rootNode.getChildren().add(depNode);
depNode.getChildren().add(empLeaf);
}
}
stage.setTitle("Tree View Sample");
VBox box = new VBox();
final Scene scene = new Scene(box, 400, 300);
scene.setFill(Color.LIGHTGRAY);
TreeView<String> treeView = new TreeView<String>(rootNode);
treeView.setShowRoot(true);
treeView.setEditable(true);
box.getChildren().add(treeView);
stage.setScene(scene);
stage.show();
}
}
class Employee {
private final SimpleStringProperty name;
private final SimpleStringProperty department;
public Employee(String name, String department) {
this.name = new SimpleStringProperty(name);
this.department = new SimpleStringProperty(department);
}
public String getName() {
return name.get();
}
public void setName(String fName) {
name.set(fName);
}
public String getDepartment() {
return department.get();
}
public void setDepartment(String fName) {
department.set(fName);
}
}