Setting a Leaf in JTree(Urgent Please)
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gautam_Mehta
Posted On:   Wednesday, July 4, 2001 03:32 AM

Hi,

I am having two different type of nodes
1.which are always leaf i.e. can not have children.

2. Which can have children and may be leaf.

So i want to distinguish these 2 type of nodes so that the user may know which can have children and which can not. This difference can be either with a color or icon or anything. I have set setAllowsChildren(true/false), correspondingly also.DefaultTreeCellRenderer won't work ciz that too will set the color of all the leaves same.
SO
if anybody can help me out, i'll be thankful
regards
Gautam

Re: Setting a Leaf in JTree(Urgent Please)

Posted By:   Aparna_Chaturvedi  
Posted On:   Wednesday, July 4, 2001 06:08 AM

what u can do is create a class in which u have the infrmation of nodes
with that class u can define a treenode with 2 parameters in which one is the name of node and the other is the type of node .so for the 2 nodes u have 2 types.all child nodes have of a particular typ eof tree will be of same type as that of parent so while creating child nodes the first parameter that is the name of node changes while the type remains same as that of the parent .so by mentioning the type u can add child nodes only to that particular node.

for example


create class node


public class Nodes


{

public String strNodeType;


public String strNodeName;



public Nodes(String NodeName,String NodeType)


{
strNodeType = NodeType;


strNodeName = NodeName;
}


then when ur creating the nodes in tree class create them as follows


DefaultMutableTreeNode node1 = new DefaultMutableTreeNode ("name1","nodeType1");


DefaultMutableTreeNode node2 = new DefaultMutableTreeNode ("name1","nodeType2");


then u can say like

if (strNodeType == nodeType1)


setIcon(img);


by this if node1 has child nodes all child nides will have the same icon as that of node1 provided if u create the child nodes as follows


DefaultMutableTreeNode child1= new DefaultMutableTreeNode ("child1","nodeType1");


DefaultMutableTreeNode child2= new DefaultMutableTreeNode ("child1","nodeType1");


and so on

can u give me reply if this reply worked or not

About | Sitemap | Contact