Java - JTree 不遵循路径层次结构
Java - JTree not following Path hierarchy
我需要创建一个树节点,它使用 HashMap 构建文件树,HashMap 的键是路径,它的值是文件名。我已经实现了分解键值以构建层次结构的代码:
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());
}
}
但由于某些我无法识别的原因,它不起作用。我仍然得到以下结果:
谁能告诉我我的代码实现哪里做错了?
问题似乎是您试图使用 /
拆分 Windows 文件路径,除了 Windows 文件路径由 \
分隔,这当然也是正则表达式的转义字符
你可以...
使用key.replace(File.separatorChar, '/').split("/")
将\
更改为/
并拆分
或者...
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));
//...
}
或者...
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();
//...
}
我需要创建一个树节点,它使用 HashMap 构建文件树,HashMap 的键是路径,它的值是文件名。我已经实现了分解键值以构建层次结构的代码:
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());
}
}
但由于某些我无法识别的原因,它不起作用。我仍然得到以下结果:
谁能告诉我我的代码实现哪里做错了?
问题似乎是您试图使用 /
拆分 Windows 文件路径,除了 Windows 文件路径由 \
分隔,这当然也是正则表达式的转义字符
你可以...
使用key.replace(File.separatorChar, '/').split("/")
将\
更改为/
并拆分
或者...
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));
//...
}
或者...
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();
//...
}