使用 Java 中的树视图实现具有层次结构的文件系统视图

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

我想创建一个显示 mod 文件夹(根目录)和里面的 mod 的树视图,但不显示 mod 文件夹的内容。现在我只能让它显示所有 mod 级别,但我认为它是我的方法 BuildChildren()。任何帮助将不胜感激。

我已经包含了下面的代码。

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;
}

}

如有任何帮助,我们将不胜感激。我认为它肯定是 buildChildren 方法,因为对于每个循环来说,它只是一种构建。我不知道什么是更好的方法来做到这一点。干杯。

我已经这样做了。您可以根据自己的需要进行修改:

private void createTreeView(String dirPath) {
    TreeItem<Object> tree = new TreeItem<>(dirPath.substring(dirPath.lastIndexOf(File.separator) + 1), new ImageView(icon));
    List<TreeItem<Object>> dirs = new ArrayList<>();
    try {
        DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(dirPath));
        for (Path path : directoryStream) {
            if (Files.isDirectory(path)) {
                String pathString = path.toString();
                TreeItem<Object> subDirectory = new TreeItem<>(pathString.substring(pathString.lastIndexOf(File.separator) + 1), new ImageView(icon));
                getSubLeafs(path, subDirectory);
                dirs.add(subDirectory);
            }
        }

        tree.getChildren().addAll(dirs);
    } catch (IOException e) {
        e.printStackTrace();
    }

    treeView.getSelectionModel().selectedItemProperty().addListener((obs, oldVal, newVal) -> {
        imageNumber = 1;
        StringBuilder pathBuilder = new StringBuilder();
        for (TreeItem<String> item = (TreeItem<String>) treeView.getSelectionModel().getSelectedItem(); item != null; item = item.getParent()) {

            pathBuilder.insert(0, item.getValue());
            pathBuilder.insert(0, "/");
        }
        String path = pathBuilder.toString();
        populateImageView(path.substring(1) + "/");
    });

    tree.setExpanded(true);
    treeView.setRoot(tree);
    treeView.setShowRoot(true);
}

private void getSubLeafs(Path subPath, TreeItem<Object> parent) {
    try {
        DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(subPath.toString()));
        for (Path subDir : directoryStream) {
            if (Files.isDirectory(subDir)) {
                String subTree = subDir.toString();
                TreeItem<Object> subLeafs = new TreeItem<>(subTree, new ImageView(icon));
                subLeafs.setValue(subTree.substring(subTree.lastIndexOf(File.separator) + 1));
                getSubLeafs(subDir, subLeafs);
                parent.getChildren().add(subLeafs);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}