Re: Advanced: Populating, Printing Tree Structure

From: Barry White (love_at_walrus.org)
Date: 02/22/04


Date: Sun, 22 Feb 2004 21:33:22 +0000

Hi again,

perhaps have your node extend DefaultMutableTreeNode() and build up a
DefaultTreeModel from the nodes. You could use a HashMap to map ID's to
nodes so you can find a parent node when you need to add a new child.

Look at:
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#data

You coult test your code with a Swing JTree too :)

Barry

Steve Johnson wrote:
> Barry White wrote:
>
>>Hi Steve,
>>
>>have you looked at the javax.swing.tree package. I know it's not a
>>swing application but the tree model framework is already in place, no
>>need to reinvent the wheel.
>>
>>DefaultTreeModel
>>DefaultMutableTreeNode
>
>
> Yes, I have looked into those APIs, but still couldn't figure out how to
> use them to any advantage with a recursive algorithm. If you have an
> example of using those APIs for my scenario, that would be great.
>
>
>
>
>
>>>From a general design point of view you could make better use of the
>>Collections framework:
>
>
> [snip]
>
>
>>SUGGESTION:
>>
>>public Node findNode( Integer nodeID, Collection nodeList )
>>{
>> Iterator iter = nodeList.iterator();
>> while (iter.hasNext())
>> {
>> Node node = (Node)iter.next();
>> Map dataRow = (Map)node.getObject();
>> Integer currID = (Integer)dataRow.get("ID");
>>
>> if( nodeID.equals(currID) )
>> {
>> return node;
>> }
>>
>> findNode( nodeID, node.getChildList() );
>> }
>>
>> return null; //couldn't find node
>>}
>
>
> Thanks, this sounds like a good idea.
>
>
> The primary obstacle is still populating and printing that tree, and I'm
> completely stuck.



Relevant Pages