如何在javafx TreeView中使目录可扩展 [英] How to make directories expandable in javafx TreeView

查看:166
本文介绍了如何在javafx TreeView中使目录可扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我不知道如何解决,也无法在网上找到一些线索,但问题应该是微不足道的......

I have a problem which I am not sure how to solve and could not find some clues online as well, but the problem should be trivial...

I有一个 TreeView< File> 我想根据给定路径填写目录和文件列表。问题是目录被添加到树中,但是无法展开,因此我无法在里面显示文件。

I have a TreeView<File> which I would like to fill with list of directories and files based on a given path. The problem is that the directories are added to the tree, but cannot be expanded, thus I cannot display the files inside.

这是我的一些简单的控制器代码:

Here is some of my humble controller code:

public class MainViewController implements Initializable {
    @FXML // fx:id="filesTree"
    private TreeView<File> filesTree;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        File currentDir = new File("src/xslt"); // current directory
        findFiles(currentDir);
    }

    public void findFiles(File dir) {
    TreeItem<File> root = new TreeItem<>(new File("Files:"));
    root.setExpanded(true);
    try {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("directory:" + file.getCanonicalPath());
                findFiles(file);
            } else {
                System.out.println("     file:" + file.getCanonicalPath());
                root.getChildren().add(new TreeItem<>(file));
            }
            root.getChildren().add(new TreeItem<>(file));
        }

        filesTree.setRoot(root);
        } catch (IOException e) {
           e.printStackTrace();
        }
    }

}

我的 FXML 视图非常简单 - 只需一个 AnchorPane ,其中 TreeView 。但如果需要,我也可以分享它。

And my FXML view is really simple - just an AnchorPane with a TreeView. But I can share it as well if needed.

所以问题是 - 如何使目录可扩展?
我发现 setExpanded(true)方法但不同。

So the questions is - how do I make the directories expandable? I found setExpanded(true) method but that is different.

推荐答案

基本上,递归方法必须在每次找到目录时创建新的根。

Basically, the recursive method has to create a new root every time you find a directory.

private void findFiles(File dir, TreeItem<File> parent) {
    TreeItem<File> root = new TreeItem<>(dir);
    ...
}

root 必须作为父级发送到下一级别。

And this root has to be sent as parent for the next level.

    if (file.isDirectory()) {
        System.out.println("directory:" + file.getCanonicalPath());
        findFiles(file,root);
    }

最后,只有在最高级别,我们才将此根设置为treeView:

Finally, only at the top most level we set this root as root of the treeView:

if(parent==null){
    filesTree.setRoot(root);
}

在内层:

else {
    parent.getChildren().add(root);
}

因此,经过这几次调整,这应该有效:

So after these few adjustements, this should be working:

@FXML private TreeView<File> filesTree;

@Override
public void initialize(URL url, ResourceBundle rb) {
    File currentDir = new File("src/xslt"); // current directory
    findFiles(currentDir,null);
}

private void findFiles(File dir, TreeItem<File> parent) {
    TreeItem<File> root = new TreeItem<>(dir);
    root.setExpanded(true);
    try {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("directory:" + file.getCanonicalPath());
                findFiles(file,root);
            } else {
                System.out.println("     file:" + file.getCanonicalPath());
                root.getChildren().add(new TreeItem<>(file));
            }

        }
        if(parent==null){
            filesTree.setRoot(root);
        } else {
            parent.getChildren().add(root);
        }
    } catch (IOException e) {
       e.printStackTrace();
    }
} 

这篇关于如何在javafx TreeView中使目录可扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆