javajavafxopenjfxtreetableview

Speed up adding data to JavaFX TreeTableView


I'm using JavaFX/OpenJFX for GUI in Java, and need to populate TreeTableView with millions of items. However, it currently takes a very long time to do it. Is there a way to speed it up?

Here is my code:

List<TreeItem<FeatureTableItem>> rootChildren = treeTableView.getRoot().getChildren();
rootChildren.clear();
for (Feature feature : features) {
    TreeItem<FeatureTableItem> featureItem = new TreeItem<>(feature);
    rootChildren.add(featureItem);
}

Solution

  • Build a separate List, and instead of using rootChildren.clear() and rootChildren.add, use setAll.

    Each time you modify the list of children, the TreeTableView has to update its own layout. By using setAll, you will cause the TreeTableView do that once, instead of millions of times.

    List<TreeItem<FeatureTableItem>> rootChildren = new ArrayList<>();
    for (Feature feature : features) {
        TreeItem<FeatureTableItem> featureItem = new TreeItem<>(feature);
        rootChildren.add(featureItem);
    }
    
    treeTableView.getRoot().getChildren().setAll(rootChildren);