Zolf
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(dslamColle
Collections.sort(dslamColl
mergeCollection(coCollect,
Collections.sort(coCollect
mergeCollection(cityCollec
DefaultMutableTreeNode root = new DefaultMutableTreeNode("")
DefaultTreeModel dtm = new DefaultTreeModel(root);
for (int k = 0; k < cityCollect.size(); k++)
{
root.add((DefaultMutableTr
//System.out.println("tree
}
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(child
for (int i = 0; i < children.size(); i++)
{
DefaultMutableTreeNode chNode = (DefaultMutableTreeNode)ch
//System.out.println(chNod
BaseRow chRow = (BaseRow)chNode.getUserObj
//System.out.println("Node
chId = chRow.getForeignKey();
//System.out.println("Fore
int tmpIndex = pIndex;
boolean fnd = false;
for (int j = tmpIndex; j < parents.size(); j++)
{
DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)pa
BaseRow pRow = (BaseRow)pNode.getUserObje
//System.out.println("Pare
pId = pRow.getPrimaryKey();
//System.out.println("Par Primary "+pId);
if (pId == chId)
{ // ooops! find parent for this child
//System.out.println("Insi
pNode.add(chNode);
fnd = true;
break;
}
tmpIndex++;
//System.out.println(" +++ ");
}
if (fnd && tmpIndex != pIndex)
{
//System.out.println("Insi
pIndex = tmpIndex;
}
}
}
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?
asd has a +. Do you want it to collapse or expand?
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
ASKER
collapse i mean open parent node
expand i mean close parent node
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.
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.
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)pa
DefaultMutableTreeNode zzz = new DefaultMutableTreeNode("zz
BaseRow pRow = (BaseRow)pNode.getUserObje
//System.out.println("Pare
pId = pRow.getPrimaryKey();
//System.out.println("Par Primary "+pId);
if (pId == chId)
{ // ooops! find parent for this child
//System.out.println("Insi
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)pa rents.get( j);
DefaultMutableTreeNode zzz = new DefaultMutableTreeNode("zz z");
pNode.add(zzz);
BaseRow pRow = (BaseRow)pNode.getUserObje ct();
//System.out.println("Pare nt Node "+pRow);
pId = pRow.getPrimaryKey();
//System.out.println("Par Primary "+pId);
if (pId == chId)
{ // ooops! find parent for this child
//System.out.println("Insi de IF ");
zzz.add(pNode);
fnd = true;
break;
}
for (int j = tmpIndex; j < parents.size(); j++)
{
DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)pa
DefaultMutableTreeNode zzz = new DefaultMutableTreeNode("zz
pNode.add(zzz);
BaseRow pRow = (BaseRow)pNode.getUserObje
//System.out.println("Pare
pId = pRow.getPrimaryKey();
//System.out.println("Par Primary "+pId);
if (pId == chId)
{ // ooops! find parent for this child
//System.out.println("Insi
zzz.add(pNode);
fnd = true;
break;
}
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
you need both
ASKER
what do you mean???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ;)
ASKER
yes but i create the
DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)pa
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)pa rents.get( 0);
eg. to 1st pareent
DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)pa
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
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("DS LAM");
DefaultMutableTreeNode pNode = new DefaultMutableTreeNode();
pNode.add(dslam);
for (int j = tmpIndex; j < parents.size(); j++)
{
pNode = (DefaultMutableTreeNode)pa rents.get( j);
//pNode.add(dslam);
BaseRow pRow = (BaseRow)pNode.getUserObje ct();
//System.out.println("Pare nt Node "+pRow);
pId = pRow.getPrimaryKey();
//System.out.println("Par Primary "+pId);
if (pId == chId)
{ // ooops! find parent for this child
//System.out.println("Insi de IF ");
dslam.add(chNode);
fnd = true;
break;
}
tmpIndex++;
//System.out.println(" +++ ");
}
-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("DS
DefaultMutableTreeNode pNode = new DefaultMutableTreeNode();
pNode.add(dslam);
for (int j = tmpIndex; j < parents.size(); j++)
{
pNode = (DefaultMutableTreeNode)pa
//pNode.add(dslam);
BaseRow pRow = (BaseRow)pNode.getUserObje
//System.out.println("Pare
pId = pRow.getPrimaryKey();
//System.out.println("Par Primary "+pId);
if (pId == chId)
{ // ooops! find parent for this child
//System.out.println("Insi
dslam.add(chNode);
fnd = true;
break;
}
tmpIndex++;
//System.out.println(" +++ ");
}
ASKER
any idea....objects.
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;
}
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("DS
for (int i = 0; i < children.size(); i++)
{
DefaultMutableTreeNode chNode = (DefaultMutableTreeNode)ch
BaseRow chRow = (BaseRow)chNode.getUserObj
//System.out.println("Node
chId = chRow.getForeignKey();
//System.out.println("Fore
int tmpIndex = pIndex;
boolean fnd = false;
for (int j = tmpIndex; j < parents.size(); j++)
{
DefaultMutableTreeNode pNode = (DefaultMutableTreeNode)pa
pNode.add(dslam);
BaseRow pRow = (BaseRow)pNode.getUserObje
ASKER
got it :)