package com.sibvisions.javafx; import java.util.ArrayList; import javafx.application.Application; import javafx.application.Launcher; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import com.javafx.preview.control.TreeView; import com.javafx.preview.control.TreeView.TreeElement; public class TreeApp extends Application { public static void main(String[] pArgs) { Launcher.launch(TreeApp.class, pArgs); } public void start(Stage pStage) { TreeView tree = new TreeView(); tree.setRoot(new TreeRoot()); tree.setShowRoot(true); BorderPane bpane = new BorderPane(); bpane.setCenter(tree); Scene scene = new Scene(bpane); pStage.setScene(scene); pStage.setVisible(true); pStage.setFocused(true); pStage.setWidth(500); pStage.setWidth(800); } @SuppressWarnings("unchecked") class TreeRoot implements TreeElement { private ObservableList list = new TreeItems(this); @Override public ObservableList getChildren() { return list; } @Override public Node getGraphic() { return null; } @Override public TreeElement getParent() { return null; } @Override public Object getValue() { return "ROOT"; } @Override public boolean isLeaf() { return false; } } @SuppressWarnings("unchecked") class TreeItems extends DummyLoadOnDemandSequence { private TreeElement parent; private ArrayList liElements = new ArrayList(); boolean bLoaded = false; public TreeItems(TreeElement pParent) { parent = pParent; } public Object get(final int index) { if (liElements.size() > index) { return liElements.get(index); } else { if (!bLoaded) { bLoaded = true; System.out.println("Load"); } final Car car = (Car)super.get(index); TreeElement element = new TreeElement() { ObservableList list = null; @Override public ObservableList getChildren() { if (list == null) { if ((parent.getParent() == null || parent.getParent().getParent() == null) && index < 5) { list = new TreeItems(this); } else { list = FXCollections.observableArrayList(); } } return list; } @Override public Node getGraphic() { return null; } @Override public TreeElement getParent() { return parent; } @Override public Object getValue() { return car.name; } @Override public boolean isLeaf() { return getChildren().isEmpty(); } }; liElements.add(element); return element; } } } }