아직이 솔루션을 테스트하지는 않았지만 작동해야한다고 생각합니다. 프라임 쇼케이스의 예제는 모두 하드 코딩되어 있습니다. 동적으로 만들려면 배열로 작업해야합니다.
은 그래서 예를 들어 아래의 하드 코드의 섹션 :
TreeNode documents = new DefaultTreeNode(new Document("Documents", "-", "Folder"), root);
TreeNode pictures = new DefaultTreeNode(new Document("Pictures", "-", "Folder"), root);
TreeNode music = new DefaultTreeNode(new Document("Music", "-", "Folder"), root);
는 교체 할 수 있습니다 :
Map<String, TreeNode> rootNodes = new HashMap<String, TreeNode>();
//Retrieve the list of root Nodes. eg Tables in the database.
for(Table table : databaseTables){
//table.getTableName() will be the name of the node, it could be something like "music" or "documents"
rootNodes.add(table.getTableName(), new DefaultTreeNode(table.getTableName(), new Document......, root);
}
내가지도에 추가 할 이유는 그래서 당신은 그들을 참조 할 경우 , 키를 참조로 사용할 수 있습니다. 지금 당신은이 방법을 사용하여 예를
//Create a new map
Map<String, TreeNode> pictureNodes = new HashMap<String, TreeNode>();
//now loop through your picture objects
for(picture : pictures){
pictureNode.add(new DefaultTreeNode(picture.getName(), ......., root.get("pictures"));
}
의 사진 노드에 노드를 추가하려면 할 수 있습니다 재귀 적으로 가능한 한 많은 계층 구조를 통해 루프. 저는 현재 사무실에 있기 때문에이 코드를 테스트 할 수 없었습니다 (단지 메모장 + +). 그러나 그것을 시험해 보라. 만일 당신이 아직도 붙이면, 내가 몇 시간 내에 집에 도착했을 때 더 상세한 것을 할 수있다.
자식 컬렉션 대신 부모에 대한 참조를 저장하는 것이 더 최적화되었습니다. 이렇게하면 참조 컬렉션 대신 하나의 참조 만 가질 수 있습니다. 관계를 유지하는 것이 더 쉽습니다. 데이터베이스에서도 이와 유사합니다. 어쩌면 이것은 그 뒤에 어떤 논리를 설명합니다 ;-) – siebz0r