import com.sun.javafx.application.PlatformImpl; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.stage.Stage; import java.util.Timer; import java.util.TimerTask; /** * @author Alexander Gulko */ public class TestTreeView3 extends TreeView { private TreeItem root = new TreeItem("root"); private static int nodesCount = 0; public TestTreeView3() { setRoot(root); setShowRoot(true); root.getChildren().add(createChild()); new Timer().schedule(new TimerTask() { @Override public void run() { PlatformImpl.runLater(new Runnable() { @Override public void run() { root.setExpanded(!root.isExpanded()); if (!root.isExpanded()) { root.getChildren().clear(); root.getChildren().add(createChild()); System.out.println(nodesCount); } } }); } }, 100, 200); } private TreeItem createChild() { return new TreeItem(String.valueOf(nodesCount++)); } public static class TestTreeApp3 extends Application { @Override public void start(final Stage stage) throws Exception { stage.setResizable(true); stage.setTitle("Tree view"); Scene scene = new Scene(new TestTreeView3(), 400, 600); stage.setScene(scene); stage.setVisible(true); } public static void main(final String[] args) { try { Application.launch(TestTreeApp3.class, args); } catch (Exception e) { e.printStackTrace(); } } } }