简体   繁体   English

Java - JTree 不遵循路径层次结构

[英]Java - JTree not following Path hierarchy

I need to create a tree node that uses a HashMap to build a tree of files, the key of the HashMap is the Path and it's Value is the File Name.我需要创建一个使用 HashMap 来构建文件树的树节点,HashMap 的键是路径,它的值是文件名。 I've implemented a code that breaks down the key value to build the hierarchy:我已经实现了一个代码,它分解了构建层次结构的键值:

public void createNode(HashMap<String, String> map) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("SQL Scripts");
        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        tree.setModel(treeModel);
        Set<String> keys = map.keySet();
        Iterator<String> it = keys.iterator();
        while (it.hasNext()) {
            String key = it.next();
            String value = map.get(key);
            String[] path = key.split("/");
            DefaultMutableTreeNode parent = root;
            for (int i = 0; i < path.length; i++) {
                boolean found = false;
                int index = 0;
                Enumeration e = parent.children();
                while (e.hasMoreElements()) {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
                    if (node.toString().equals(path[i])) {
                        found = true;
                        break;
                    }
                    index++;
                }
                if (!found) {
                    DefaultMutableTreeNode child = new DefaultMutableTreeNode(path[i]);
                    treeModel.insertNodeInto(child, parent, index);
                    parent = child;
                } else {
                    parent = (DefaultMutableTreeNode) treeModel.getChild(parent, index);
                }
            }
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(value);
            treeModel.insertNodeInto(child, parent, parent.getChildCount());
        }
    }

But for some reason that I can't indentify, it is not working.但是由于某种我无法识别的原因,它不起作用。 I still get the following result:我仍然得到以下结果:

createNode 方法的结果

Could anyone tell me what I did wrong on my code implementation?谁能告诉我我在代码实现上做错了什么?

The problem seems to be that you're trying to split a Windows file path using / , except, Windows file paths are separated by \ , which of course is also an escape character of regular expressions 😓问题似乎是您试图使用/拆分 Windows 文件路径,除了 Windows 文件路径由\分隔,这当然也是正则表达式的转义字符😓

You could...你可以...

Use key.replace(File.separatorChar, '/').split("/") to change the \ to / and the split on it使用key.replace(File.separatorChar, '/').split("/")\更改为/并对其进行拆分

Or...或者...

File file = new File(key);
Path path = file.toPath();
// Path path = Paths.get(key);
for (int index = 0; index < path.getNameCount(); index++) {
    String subPath = path.getName(index));
    //...
}

Or...或者...

File file = new File(key);
Path path = file.toPath();
// Path path = Paths.get(key);
Iterator<Path> it = path.iterator();
while (it.hasNext()) {
    Path next = it.next();
    String subPath = path.getFileName();
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM