我正在尝试JTree
从输出构建文件夹结构。这是方法。
- 我叫系统
- 获取文件夹列表
- 遍历文件夹列表并创建一个列表
- 从列表中创建文件夹结构
当文件夹数量较少时,这工作正常,但是当文件夹数量更多时,它会引发堆栈溢出错误。
private static DefaultMutableTreeNode builtTreeNode(String comptype) {
try{
List<Node> nodeList = new ArrayList<>();
//Defin the configuration context
APIObject[] fol = //Call the other system to get folders;
DefaultMutableTreeNode dmtNode = null;
System.out.println(" Folder Size " +fol.length);
for (int i = 0; i < fol.length; i++) {
if(getParentFolder().getName() == null && !getParentFolder().getIsActive()){
dmtNode = new DefaultMutableTreeNode(df.getName().toString());
nodeList.add(new Node(df.getName().toString()));// Set the ROOT folder in nodelist
}
if(getParentFolder().getName() != null ){
//nodeList.add(new Node("Child", "Parent"));
//add remaining folders to nodelist
nodeList.add(new Node(df.getName(), df.getParentFolder().getName()));
}
}
findChild(dmtNode, nodeList); //
return dmtNode;
}
private static void findChild(DefaultMutableTreeNode parent, List<Node> list) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).parent != null && list.get(i).parent.equals(parent.toString())) {
DefaultMutableTreeNode child = new DefaultMutableTreeNode();
child = new DefaultMutableTreeNode(list.get(i).label);
parent.add(child); // Getting exception from this line of code
findChild(child, list); // child of child
}
}
}