[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

counting the nodes in JTree

Posted on 1999-01-06
17
Medium Priority
?
463 Views
Last Modified: 2008-03-17
I want to be able to count the nodes below a certain level in a JTree component,

how do i do this?
0
Comment
Question by:plaskowj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 150 total points
ID: 1230524
i haven't tested the code, but you should see the idea

int countAllSubChilds(TreeNode node)
{
// if the node is leaf return 1
   if (node.getChildCount() == 0) return 1;

// if the node is not leaf - count all the childs
   int count = 0;
   for (int i = 0; i < node.getChildCount(); i++)
   {
       count += countAllSubChilds(node.getChildAt(i));
   }

   return (count + 1); // add 1 for this node (if you count all the nodes not only leaves)

//   return count; // use this if you want to count only tree leaves
}


hope this helps
  heyhey

P.S. What's going on with your "JTree simple question"
0
 

Author Comment

by:plaskowj
ID: 1230525
ok i have set up my JTree in a simple way:

DefaultMutableTreeNode top = new DefaultMutableTreeNode("Administration");
  DefaultMutableTreeNode D1 = new DefaultMutableTreeNode("Department1");
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?

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230526
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) ;

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:plaskowj
ID: 1230527
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?
0
 

Author Comment

by:plaskowj
ID: 1230528
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
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230529
this how you can get selected node (extracting it from the TreePath)

TreePath path = tree.getSelectionPath();
TreeNode node = (TreeNode)path.getLastPathComponent();

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230530
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

0
 

Author Comment

by:plaskowj
ID: 1230531
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
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230532
no, no, no problems ;-)

as I said, I was just curious ....

What's going on with your "JTree simple question" ?

0
 

Author Comment

by:plaskowj
ID: 1230533
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 ?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230534
you can easy put your own Leaf icon using

UIManager.put("Tree.leafIcon", 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)

0
 

Author Comment

by:plaskowj
ID: 1230535
can you give me an example of cell rendering?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230536
check this question
http://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 ...
0
 

Author Comment

by:plaskowj
ID: 1230537
cheers, but one last thing


where do i put this code?
UIManager.put("Tree.leafIcon", new ImageIcon("icon.gif"));


0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230538
probably you can put this code anywhere you want (it changes the default TreeLeaf image 'forever')
0
 

Author Comment

by:plaskowj
ID: 1230539
so why didn't it work- i set the icon.gif- to an icon which i placed in my project
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230540
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)


0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question