'Using Tree View in Java to implement a view of a filesystem with hierarchy

I'd like to create a tree view that displays a mod folder (the root directory) and the mods inside, but not the mod folders contents. Right now I can only get it to display all of the mod levels but I think its my method BuildChildren(). Any help would be appreciated.

I've included the code below.

import java.io.File;
import javafx.scene.control.TreeView;
import javafx.scene.control.TreeItem;
import javafx.scene.Node;



    public class SimpleFileTreeItem extends TreeItem<File> {

    public SimpleFileTreeItem(File f) {
        super(f);
    }
 
    @Override
    public ObservableList<TreeItem<File>> getChildren() {
        if (isFirstTimeChildren) {
                        isRoot = true;
            isFirstTimeChildren = false;
 
            /*
             * First getChildren() call, so we actually go off and determine the
             * children of the File contained in this TreeItem.
             */
            super.getChildren().setAll(buildChildren(this));
        }
        return super.getChildren();
    }

    public boolean isBranch() {
        if (isLeaf == false && isRoot == false) {
                        isRoot = false;
            isFirstTimeLeaf = true;
            File f = (File) getValue();
            isBranch = f.isFile();
        }
 
        return isBranch;
    }


    public boolean isLeaf() {
        if (isBranch == false && isRoot == false) {
                        isRoot = false;
            isFirstTimeLeaf = true;
            File f = (File) getValue();
            isLeaf = f.isFile();
        }
 
        return isLeaf;
    }

    public boolean isLeafTip() {
        if (isBranch == false && isRoot == false) {
                        isRoot = false;
            isFirstTimeLeaf = false;
            File f = (File) getValue();
            isLeafTip = f.isFile();
        }
 
        return isLeafTip;
    }

    private ObservableList<TreeItem<File>> buildChildren(TreeItem<File> TreeItem) {
        File f = TreeItem.getValue();
        if (f != null && f.isDirectory()) {
                        isRoot = true;
            File[] files = f.listFiles();
            if (files != null && isLeaf == false && isRoot == true) {
                ObservableList<TreeItem<File>> childFile = FXCollections.observableArrayList();
                                isBranch = true;
 
                                if(isBranch == true && isFirstTimeLeaf == false) {   
                                
                for (File file : files) {
                    childFile.add(new SimpleFileTreeItem(file));
                }
                                    return childFile;
                                }
                        }
                }    
        return FXCollections.emptyObservableList();
    }
 
    private boolean isFirstTimeChildren = true;
    private boolean isRoot = true;
    private boolean isFirstTimeLeaf = false;
    private boolean isLeaf = false;
        private boolean isBranch = false;
        private boolean isLeafTip;
}

}

Any help would be greatly appreciated. I think its the buildChildren method for sure, as that for each loop is just sort of building as it goes. I don't know what would be a better way to do this though. Cheers.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source