Results 1 to 5 of 5

Thread: How to use Trees in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    122

    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

  2. #2
    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.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    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.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    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);

  5. #5
    Join Date
    Jul 2006
    Posts
    289

    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

Similar Threads

  1. Replies: 3
    Last Post: 25-01-2012, 04:52 PM
  2. What are Giant Trees in FarmVille
    By Lipika in forum Video Games
    Replies: 5
    Last Post: 20-02-2011, 05:32 AM
  3. FrontierVille: How to plant the peanut trees
    By Samudrasen in forum Video Games
    Replies: 5
    Last Post: 12-02-2011, 07:44 PM
  4. Ornamental Trees in FarmVille
    By Lizbeth in forum Video Games
    Replies: 5
    Last Post: 11-02-2011, 06:54 AM
  5. FarmVille : More than 60 presents / Need More Trees
    By Danelle in forum Video Games
    Replies: 4
    Last Post: 05-02-2011, 03:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,373,925.59828 seconds with 16 queries