Solved

building JTree

Posted on 2006-06-26
23
317 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
  • 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:mayankeagle
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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 92

Accepted Solution

by:
objects earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now