Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



How to use Trees in Java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-02-2010
- Empty Shell -'s Avatar
Member
 
Join Date: Aug 2006
Posts: 112
How to use Trees in Java?

Hello friends,
I am new to this forum, so kindly ignore my mistakes. I have done some basic programs in Java. Now I want to give a hierarchical data look to the user. Its just same like the one that we use in Windows Explorer. Later I came to know that the trees function can do this thing.! But I don't know how to use it. So can anyone please tell me how to use Trees in Java.?? Hoping that someone would help me soon.!!
__________________
As you simplify your life, the laws of the universe will be simpler; solitude will not be solitude, poverty will not be poverty, nor weakness.Henry David Thoreau
Reply With Quote
  #2  
Old 12-02-2010
Allan.d's Avatar
Member
 
Join Date: Mar 2008
Posts: 672
Re: How to use Trees in Java?

A control that displays a set of hierarchical data as an outline. If you want to display hierarchical data in Java, then you would have to use JTree class. You should know that there is no data in the JTree object, it only provides a view of the data. You can identify a specific node in a tree by TreePath. You can also identify that by the row, because each row in the display area displays one node. Since you are new you should not be knowing the node.!! Each row contains exactly one item of data which can be known as node. Every tree has a root node from which all nodes descend.
Reply With Quote
  #3  
Old 12-02-2010
Member
 
Join Date: Nov 2008
Posts: 1,193
Re: How to use Trees in Java?

You will have to create a TreeNode while initializing a tree. And if you want to make node a leaf, then you would have to invoke setAllowsChildren(false). A node can either have children or not. Leaf nodes are the nodes that can't have children. While branch nodes can have any number of children. I think that once you understand the concept of the Tree you can do the coding for the same. Hope that you would have got the basic points.
Reply With Quote
  #4  
Old 12-02-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
Re: How to use Trees in Java?

The following sample of a code will help you for creating the JTree object :
Code:
DefaultMutableTreeNode top = new DefaultMutableTreeNode("This is Trial of Tree");
createNodes(top);
final JTree tree = new JTree(top);
...
JScrollPane treeView = new JScrollPane(tree);
Reply With Quote
  #5  
Old 12-02-2010
Viensterrr's Avatar
Member
 
Join Date: Jul 2006
Posts: 232
Re: How to use Trees in Java?

Once you start using the tree, there are many times when you would have to creates the nodes under the root node. I thought that providing the code for same would be helpful. So here is the code for creating the nodes under the root node :
Code:
private void createNodes(DefaultMutableTreeNode top) {
    DefaultMutableTreeNode category = null;
    DefaultMutableTreeNode book = null;

    category = new DefaultMutableTreeNode("Names of Cars");
    top.add(category);

    car = new DefaultMutableTreeNode(new carInfo
        ("Mustang.html"));
    category.add(car);

    car = new DefaultMutableTreeNode(new carInfo
        ("Lamborghini Diablo.html"));
    category.add(car);

    car = new DefaultMutableTreeNode(new carInfo
        ("Acura.html"));
    category.add(car);

    category = new DefaultMutableTreeNode("Cars for Fans");
    top.add(category);

    car = new DefaultMutableTreeNode(new carInfo
        ("Information about Cars",
         "vm.html"));
    category.add(car);

    car = new DefaultMutableTreeNode(new carInfo
        ("Information about Cars",
         "inf.html"));
    category.add(car);
}
__________________
Signatures reduce available bandwidth
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use Trees in Java?"
Thread Thread Starter Forum Replies Last Post
Footprint can’t tell what trees are in what orchard in Farmville Chrirag 1 Video Games 6 29-08-2011 08:09 PM
What are Giant Trees in FarmVille Lipika Video Games 5 20-02-2011 05:32 AM
FrontierVille: How to plant the peanut trees Samudrasen Video Games 5 12-02-2011 07:44 PM
Ornamental Trees in FarmVille Lizbeth Video Games 5 11-02-2011 06:54 AM
FarmVille : More than 60 presents / Need More Trees Danelle Video Games 4 05-02-2011 03:20 AM


All times are GMT +5.5. The time now is 03:35 AM.