[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

building JTree


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
zolf
Asked:
zolf
  • 14
  • 8
1 Solution
 
objectsCommented:
not sure I follow, can u explain more
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> now when i click asd it will collapse futher.

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

-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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
zolfAuthor Commented:

collapse i mean open parent node
expand i mean close parent node
0
 
zolfAuthor Commented:

i want asd to collapse and show other leaf nodes inside it
0
 
objectsCommented:
>                         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
 
zolfAuthor Commented:

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
 
objectsCommented:
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
 
zolfAuthor Commented:

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
 
objectsCommented:
that doesn't add zzz to the tree, it just adds the node to zzz.
you need both
0
 
zolfAuthor Commented:

what do you mean???
0
 
objectsCommented:
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
 
zolfAuthor Commented:

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
 
objectsCommented:
cretae it *outside* your loop ;)
0
 
zolfAuthor Commented:

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
 
objectsCommented:
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
 
zolfAuthor Commented:

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
 
objectsCommented:
don't create dslam in the loop
0
 
zolfAuthor Commented:
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
 
zolfAuthor Commented:

any idea....objects.
0
 
zolfAuthor Commented:

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
 
zolfAuthor Commented:

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
 
zolfAuthor Commented:

got it :)
0

Featured Post

Technology Partners: 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!

  • 14
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now