Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

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;
                }
          }
    }
Avatar of Mick Barry
Mick Barry
Flag of Australia image

not sure I follow, can u explain more
>> now when i click asd it will collapse futher.

asd has a +. Do you want it to collapse or expand?
Avatar of Zolf

ASKER


-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

Avatar of Zolf

ASKER


collapse i mean open parent node
expand i mean close parent node
Avatar of Zolf

ASKER


i want asd to collapse and show other leaf nodes inside it
>                         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.
Avatar of Zolf

ASKER


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;
                      }
   
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;
                   }
Avatar of Zolf

ASKER


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.
that doesn't add zzz to the tree, it just adds the node to zzz.
you need both
Avatar of Zolf

ASKER


what do you mean???
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER


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
cretae it *outside* your loop ;)
Avatar of Zolf

ASKER


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.
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);
Avatar of Zolf

ASKER


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
       

don't create dslam in the loop
Avatar of Zolf

ASKER

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(" +++ ");
                }
Avatar of Zolf

ASKER


any idea....objects.
Avatar of Zolf

ASKER


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;
}
Avatar of Zolf

ASKER


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();
Avatar of Zolf

ASKER


got it :)