plaskowj
asked on
counting the nodes in JTree
I want to be able to count the nodes below a certain level in a JTree component,
how do i do this?
how do i do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
top and D1 are DefaultMutableTreeNode which is a special kind of TreeNode
you can count all the nodes from the tree using
int allNodesCount = countAllSubChilds(top) ;
you can count all the nodes below D1 using
int sublNodesCount = countAllSubChilds(D1) ;
you can count all the nodes from the tree using
int allNodesCount = countAllSubChilds(top) ;
you can count all the nodes below D1 using
int sublNodesCount = countAllSubChilds(D1) ;
ASKER
so finanly if i only wanted to count what is below D1 for example and not what is in D2 ( D2 being a sub node of D1) how whould i do this?
ASKER
i figured out how to count below D1, but how do i count sub nodes, given that i only have the path or is there a way of retrieving node name?
many thanks
many thanks
this how you can get selected node (extracting it from the TreePath)
TreePath path = tree.getSelectionPath();
TreeNode node = (TreeNode)path.getLastPath Component( );
TreePath path = tree.getSelectionPath();
TreeNode node = (TreeNode)path.getLastPath
I'm just curious ... What do you think is 'GRADE A' Answer ?
>> Name: plaskowj
>> First Login: 11/12/98
>>
>> Last 10 Grades Given
>> B C B
>> Name: plaskowj
>> First Login: 11/12/98
>>
>> Last 10 Grades Given
>> B C B
ASKER
to be honest i don't really look at the grades- the answer you gave me was great, next time i will rate it as excellent,
thanks anyway
Ps if you want to post a request for rating i will re-rate it with grade A
thanks anyway
Ps if you want to post a request for rating i will re-rate it with grade A
no, no, no problems ;-)
as I said, I was just curious ....
What's going on with your "JTree simple question" ?
as I said, I was just curious ....
What's going on with your "JTree simple question" ?
ASKER
The problem i am having is that firstly I don't want "all" the leafs to be leafs, but instead empty folders- how do i do this?
and i also want to change the leaf icon to something else, how ?
and i also want to change the leaf icon to something else, how ?
you can easy put your own Leaf icon using
UIManager.put("Tree.leafIc on", new ImageIcon("icon.gif"));
but if you want some leaves to have different icon than other leaves, you'll need to implement custom TreeCellRenderer (that is a component that knows how to render / paint JTree nodes)
UIManager.put("Tree.leafIc
but if you want some leaves to have different icon than other leaves, you'll need to implement custom TreeCellRenderer (that is a component that knows how to render / paint JTree nodes)
ASKER
can you give me an example of cell rendering?
check this question
https://www.experts-exchange.com/topics/comp/lang/java/Q.10113010
there are some usefull comments there and I'll post some 'simple' code in a few days ...
https://www.experts-exchange.com/topics/comp/lang/java/Q.10113010
there are some usefull comments there and I'll post some 'simple' code in a few days ...
ASKER
cheers, but one last thing
where do i put this code?
UIManager.put("Tree.leafIc on", new ImageIcon("icon.gif"));
where do i put this code?
UIManager.put("Tree.leafIc
probably you can put this code anywhere you want (it changes the default TreeLeaf image 'forever')
ASKER
so why didn't it work- i set the icon.gif- to an icon which i placed in my project
don't know ... it should work ...
I'll check this later and post a comment in the real question :)
(you can take a look at SampleTree example in your Swing directory - they use it too)
I'll check this later and post a comment in the real question :)
(you can take a look at SampleTree example in your Swing directory - they use it too)
ASKER
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Ad
DefaultMutableTreeNode D1 = new DefaultMutableTreeNode("De
top.add(D1);
tree = new JTree(top);
so how do i fit the above into this, where do i declare something to be a node?