【发布时间】:2011-07-01 14:52:55
【问题描述】:
我有一个包含字段 category_id、category_name 和 parent_category_id 的表。并且 parent_category_id 具有来自 category_id 的值,代表父子关系。我没有任何固定的层次结构,它可以达到 5 级或 10 级,并且没有限制。我需要一个代码来说明如何实现这个 JTree 以使事情为我工作。我应该也可以为菜单栏实现相同的功能..请帮助我..
我在谷歌上搜索后发现了这个,
Map<String, Node> idToNode = new HashMap<String, Node>();
//create nodes from ResultSet
while ( resultSet.next() ){
Node node = //create node -contains info, parent id, and its own id from ResultSet
//put node into idToNode, keyed with its id
}
//link together
Iterator<String> it = idToNode.keySet().iterator();
Node root = null;
while ( it.hasNext() ){
Node node = idToNode.get(it.next());
Node parent = idToNode.get(node.getParentId());
if ( parent == null ) {
root = node;
}else{
parent.addChild(node);
}
}
我如何对这些注释说明进行编码?
【问题讨论】: