?
Solved

building JTree

Posted on 2006-06-26
23
Medium Priority
?
325 Views
Last Modified: 2012-08-14

Hello there,

I have a JTree which is working fine and gets its data from the db and looks like this

-abc
  -qwe
    -swe
       -eew

now i want to change this structure to

-abc
   -qwe
      +asd
      +der
      +wer

now when i click asd it will collapse futher.

the two functions which is doing the first structure is below.

private void createAndInitTree()
    {
          
          mergeCollection(dslamCollect, linecardCollect);
          Collections.sort(dslamCollect, new ForeignKeyComparator());
          mergeCollection(coCollect, dslamCollect);
          Collections.sort(coCollect, new ForeignKeyComparator());
          mergeCollection(cityCollect, coCollect);
          
          DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
          
          DefaultTreeModel dtm = new DefaultTreeModel(root);
          
          for (int k = 0; k < cityCollect.size(); k++)
          {
                root.add((DefaultMutableTreeNode)cityCollect.get(k));
                //System.out.println("tree node "+cityCollect.get(k));
          }

          if(tree == null)
          {
                tree = new JTree(root);
                tree.setCellRenderer(new MyRenderer());
                tree.repaint();

                //System.out.println(" flagg    "+flag);
                //System.out.println(" inside ifffff    ");
          }
          else
          {
                tree.setModel(dtm);
                //System.out.println(" inside esleeeee    ");
          }
    }
   
    private void mergeCollection(Vector parents, Vector children)
    {
          //Supose that parent collection sorted by id,
          //and children collection sorted by parent id
          
          int pIndex = 0;
          int pId = 0;
          int chId = 0;
          //System.out.println(children.size());
          for (int i = 0; i < children.size(); i++)
          {
                DefaultMutableTreeNode chNode = (DefaultMutableTreeNode)children.get(i);
                //System.out.println(chNode);
                BaseRow chRow = (BaseRow)chNode.getUserObject();
                //System.out.println("Node "+chRow);
                chId = chRow.getForeignKey();
                //System.out.println("Foreign "+chId);
                int tmpIndex = pIndex;
                boolean fnd = false;
                
                for (int j = tmpIndex; j < parents.size(); j++)
                {
                      DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)parents.get(j);
                      BaseRow pRow = (BaseRow)pNode.getUserObject();
                      //System.out.println("Parent Node "+pRow);
                      pId = pRow.getPrimaryKey();
                      //System.out.println("Par Primary "+pId);      
                      if (pId == chId)
                      { // ooops! find parent for this child
                            //System.out.println("Inside IF ");      
                            pNode.add(chNode);
                            fnd = true;
                            break;
                      }
                      tmpIndex++;
                      //System.out.println(" +++ ");
                }
                
                if (fnd && tmpIndex != pIndex)
                {
                      //System.out.println("Inside 2 ");
                      pIndex = tmpIndex;
                }
          }
    }
0
Comment
Question by:zolf
[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
  • 14
  • 8
23 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16982479
not sure I follow, can u explain more
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16982583
>> now when i click asd it will collapse futher.

asd has a +. Do you want it to collapse or expand?
0
 

Author Comment

by:zolf
ID: 16982584

-abc
  -qwe
    -swe
       -eew

now i want to change this structure to

-abc
   -qwe
      +asd
      +der
      +wer

i want asd,der,wer to be in qwe as above.when i click asd then swe,eew should be shown

-abc
   -qwe
      -asd
          swe
          eew      
      +der
      +wer

hope i made my self clearer

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:zolf
ID: 16982590

collapse i mean open parent node
expand i mean close parent node
0
 

Author Comment

by:zolf
ID: 16982594

i want asd to collapse and show other leaf nodes inside it
0
 
LVL 92

Expert Comment

by:objects
ID: 16982641
>                         pNode.add(chNode);

you need to update pNode to be the appropriate parent to add the child to.
Can't tell from what you have posted what indictaes what the parent should be.
0
 

Author Comment

by:zolf
ID: 16990133

in my code above

-abc
  -qwe
    +swe
    +eew
    +asddd

how can i put the children of qwe into one node like this

-abc
  -qwe
    +zzz

where zzz will contain
-zzz
    +swe
    +eew
    +asddd

i tried something like this i.e i added DefaultMutableTreeNode zzz and inside the if loop i add pNode to zzz.but i dont see any node.please help


for (int j = tmpIndex; j < parents.size(); j++)
                {
                      DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)parents.get(j);
                      DefaultMutableTreeNode zzz = new DefaultMutableTreeNode("zzz");
                      BaseRow pRow = (BaseRow)pNode.getUserObject();
                      //System.out.println("Parent Node "+pRow);
                      pId = pRow.getPrimaryKey();
                      //System.out.println("Par Primary "+pId);      
                      if (pId == chId)
                      { // ooops! find parent for this child
                            //System.out.println("Inside IF ");      
                            pNode.add(chNode);
                            zzz.add(pNode);
                            fnd = true;
                            break;
                      }
   
0
 
LVL 92

Expert Comment

by:objects
ID: 16990198
u forgot to add zzz to the tree

for (int j = tmpIndex; j < parents.size(); j++)
              {
                   DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)parents.get(j);
                   DefaultMutableTreeNode zzz = new DefaultMutableTreeNode("zzz");
                   pNode.add(zzz);
                   BaseRow pRow = (BaseRow)pNode.getUserObject();
                   //System.out.println("Parent Node "+pRow);
                   pId = pRow.getPrimaryKey();
                   //System.out.println("Par Primary "+pId);    
                   if (pId == chId)
                   { // ooops! find parent for this child
                        //System.out.println("Inside IF ");    
                        zzz.add(pNode);
                        fnd = true;
                        break;
                   }
0
 

Author Comment

by:zolf
ID: 16990254

i did that,but i inserted the zzz.add(pNode) inside the if loop  if (pId == chId) which you had told me yesterday.
i want it the other way round i.e

zzz.add(pNode);

but that does not work.

i am trying to have a branch node called zzz were all the pNodes will go inside it.
0
 
LVL 92

Expert Comment

by:objects
ID: 16990262
that doesn't add zzz to the tree, it just adds the node to zzz.
you need both
0
 

Author Comment

by:zolf
ID: 16990277

what do you mean???
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16990291
your code simply creates a node (zzz) and adds other nodes to it.
nowhere do you add that node (zzz) to your tree.

for a nore to appear in a tree it needs to be added.

in the changes I made I added the following to add the node to the tree.


                   pNode.add(zzz);
0
 

Author Comment

by:zolf
ID: 16990354

i know what you mean now :). i got partly to my solution.but since i am creating the zzz inside a for loop i am getting many zzz.
how can i get only one zzz and then add zzz.add() inside the if
0
 
LVL 92

Expert Comment

by:objects
ID: 16990389
cretae it *outside* your loop ;)
0
 

Author Comment

by:zolf
ID: 16990447

yes but i create the

DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)parents.get(j); inside the for loop.if i take

pNode.add(zzz); outside the for loop it cannot recognise pNode.
0
 
LVL 92

Expert Comment

by:objects
ID: 16990520
if you don't want to add zzz to every parent, then just add it to the actual parent you want to add it to
eg. to 1st pareent

DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)parents.get(0);
0
 

Author Comment

by:zolf
ID: 16990579

i have to add zzz to each parent.but the problem now is like this

-city1
   -co1
      -dslam
           -man
      -dslam
           -tom
      -dslam
           -peter
    -co2
       -dslam2
             -man2
       -dslam2
             -tom2

it should have been

-city1
   -co
      -dslam
           -man
           -tom
           -peter

-city2
   -co
      -dslam
       

0
 
LVL 92

Expert Comment

by:objects
ID: 16990630
don't create dslam in the loop
0
 

Author Comment

by:zolf
ID: 16990655
when i create pNode.add(dslam); outside for loop i dont get anything after the parent node i.e co like this

-city1
   -co1
    -co2
    -co3


this is how i created the dslam and pNode treeNode outside like this
int tmpIndex = pIndex;
                boolean fnd = false;
                DefaultMutableTreeNode dslam = new DefaultMutableTreeNode("DSLAM");
                DefaultMutableTreeNode pNode = new DefaultMutableTreeNode();
                pNode.add(dslam);
                for (int j = tmpIndex; j < parents.size(); j++)
                {
                      pNode = (DefaultMutableTreeNode)parents.get(j);
                      
                      
                            //pNode.add(dslam);
                      
                      BaseRow pRow = (BaseRow)pNode.getUserObject();
                      //System.out.println("Parent Node "+pRow);
                      pId = pRow.getPrimaryKey();
                      //System.out.println("Par Primary "+pId);      
                      if (pId == chId)
                      { // ooops! find parent for this child
                            //System.out.println("Inside IF ");      
                            dslam.add(chNode);
                            fnd = true;
                            break;
                      }
                      tmpIndex++;
                      //System.out.println(" +++ ");
                }
0
 

Author Comment

by:zolf
ID: 16990910

any idea....objects.
0
 

Author Comment

by:zolf
ID: 16990930

i tried to only run that pNode.add(dslam); once when it comes in the second for loop.but then it only does for the first node and there is nothing after that.

if(fla)
{
pNode.add(dslam);
fla = false;
}
0
 

Author Comment

by:zolf
ID: 16998731

the problem is creating the dslam node.i have to create only one dslam object and fill it with the child node.but when i take dslam outside it does not create dslam node and its child nodes.please help

private void mergeCollections(Vector parents, Vector children)
    {
          //Supose that parent collection sorted by id,
          //and children collection sorted by parent id
          
          int pIndex = 0;
          int pId = 0;
          int chId = 0;

          DefaultMutableTreeNode dslam = new DefaultMutableTreeNode("DSLAM");

          for (int i = 0; i < children.size(); i++)
          {
                
                DefaultMutableTreeNode chNode = (DefaultMutableTreeNode)children.get(i);

                BaseRow chRow = (BaseRow)chNode.getUserObject();
                //System.out.println("Node "+chRow);
                chId = chRow.getForeignKey();
                //System.out.println("Foreign "+chId);
                int tmpIndex = pIndex;
                boolean fnd = false;
                               
                for (int j = tmpIndex; j < parents.size(); j++)
                {                     
                      DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)parents.get(j);
                      
                
                      
                      pNode.add(dslam);
                
                      BaseRow pRow = (BaseRow)pNode.getUserObject();
0
 

Author Comment

by:zolf
ID: 16998786

got it :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 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