September 04, 2010, 03:48:40 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: [Lesson] Eveything you need to know about Trees - PART1  (Read 640 times)
0 Members and 1 Guest are viewing this topic.
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« on: July 30, 2009, 01:29:40 PM »

    Table of Contents
    • Using Trees
    • Building a Tree
    • Creating a Jtree component
    • Making it select
    • Putting it all togther



    Using Trees

    A tree is basically a fancy way of using a swing component, usually they are for a list or something or used to dispay certain items in order.
    In case you didnt know trees are created from something called the Jtree class.

    Here is how a typical Tree diagram would look like, special thanks to google images for the picture:
    I would say Trees are the most hardest type of swing component there is and there is far too much to explain to this community, i would be here for days, but i'm going to make this simple for people to understand and only cover the basics, I.E how to make the above image, and how to use Trees.

    Ok, before i explain how we make a tree there are a few important terms you all should now, incase you dont i am going to tell you and explain each one.

    • Node: Each element or the "main" folder is called a Node, I.E if you take a look at the above image a Node would be javax.swing, nodes must be created for a sperate class that implements the Treenode interface. Thats all you need to know about Nodes for now.
    • Root node: This is the starting Node for every single tree, every single tree component must have this otherwise it doesnt make it a tree, this node would be linked to the above node. When you create a tree component, you pass the root node to the Jtree contructor.
    • Child Node: This is the node that appears below the other main node once clicked on, yeh rember you can have as many of these child nodes (lol), a Node can have more than one child.
    • Parent Node:This is every nodes pearent, every node must have one, if you dont have this you are a bastard etc...
    • Silbling Nodes: These are when the Children are from the same pereants
    • Leaf Node: These Nodes dont have any pearnets or children, these are normally at then end of the tree represnting the tree to be finished aka these are the loners in the family tree they have no one.
    • Path:This is the line which join's each node and thats all you guys need to know

    I suggest you keep in mind what each of the above node does becasue in the next part i am now going to teach you how to build you basic tree.




    Building a Tree

    Before you actually give the Tree aka the Branch a control you first would actually have to build the tree it displays, the easiest way to do this is to use something called [/list]
    Code
    GeSHi (java):
    1. DefultMutableTreeNode
    Created by GeSHI 1.0.7.20
    class.

    The
    Code
    GeSHi (java):
    1. DefultMutableTreeNode
    Created by GeSHI 1.0.7.20
    class bassically implements the TreeNode interface (simple aye:))
    so whoopie now you can use
    Code
    GeSHi (java):
    1. DefultMutableTreeNode
    Created by GeSHI 1.0.7.20
    Objects for any of the methods listed that are TreeNode objects, which are all of them lol....

    Ok i am now going to show u some Contructors and methods and what they actually do, these are used a lot in building trees... *yawn*

    Costructer
    Code
    GeSHi (java):
    1. DefultMutableTreeNode ()      -   This creates an EMPTY tree node or a branch Lol, call it what u want.
    Created by GeSHI 1.0.7.20

    Code
    GeSHi (java):
    1. DefultMutableTreeNode         -  This creates a tree node with a spersifc user object..
    2.   (Object userObject)
    Created by GeSHI 1.0.7.20

    Method


    Code
    GeSHi (java):
    1. void add(TreeNode child)         - This adds a child node
    Created by GeSHI 1.0.7.20

    Code
    GeSHi (java):
    1. TreeNode getFirstChild()             -     Gets the first of this type of nodes child
    Created by GeSHI 1.0.7.20

    Code
    GeSHi (java):
    1. DefultMutableTreeNode               -    Gets the next sibling duh..
    2. getNextSibling()
    Created by GeSHI 1.0.7.20

    Code
    GeSHi (java):
    1. TreeNode getParent ()                - gets the nodes parent
    Created by GeSHI 1.0.7.20

    Code
    GeSHi (java):
    1. Object getUserObject ()               - gets the user object of the node
    Created by GeSHI 1.0.7.20




    Ok i'm now gona show u the first few step to get started, the first step is you declare a DefultMutableTreeNode varible for each node that inst a leaf node:
    Code
    GeSHi (java):
    1. DefultMutableTreeModel  Ikram,  BOT1,  BOT2,
    2.                                  Happy,  Sad,  Cranky;
    Created by GeSHI 1.0.7.20


    Next you will have to create the RootNode:
    Code
    GeSHi (java):
    1. DefultMutableTreeModel root=
    2. new DefultMutableTreeModel ("Ikrams Tree Tutorial") ;
    Created by GeSHI 1.0.7.20




    This is PART 1 as i have to now leave, i will update this as soon as i get back.

    Written by _ikram
    Logged

    RuneScape Developers community: www.rspsnetwork.com
    Hextion Private Server: www.hextion.com
    Javaforums.net :: a community about Java software development.
    « on: July 30, 2009, 01:29:40 PM »

     Logged
    Arkie
    Javaforums.net Admin
    Senior Member
    *

    Reputation: 16
    Developer @ Javaforums.net
    Offline Offline
    Posts: 2620
    Referrals: 13

    WWW Awards
    « Reply #1 on: July 31, 2009, 02:25:41 AM »

    Nice tutorial and explanation of using the JTree, reputation added +1  14
    Logged

    Java and .NET developer

    To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

    Fan of http://www.retardedweblogger.com
    Oh man, too much stuff to do in so little time.

    http://img222.imageshack....707/arkietomatoesmall.jpg
    Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
    Triple D, eerste Denken Dan Doen
    _ikram
    Member
    *

    Reputation: 3
    Offline Offline
    Posts: 65
    Referrals: 0

    Awards
    « Reply #2 on: July 31, 2009, 08:14:32 AM »

    Nice tutorial and explanation of using the JTree, reputation added +1  14
    Thanks mate, i'm gona stick on these forums for a while know, best java forums yet, dw i will make this forum very active and busy.
    Logged

    RuneScape Developers community: www.rspsnetwork.com
    Hextion Private Server: www.hextion.com
    Arkie
    Javaforums.net Admin
    Senior Member
    *

    Reputation: 16
    Developer @ Javaforums.net
    Offline Offline
    Posts: 2620
    Referrals: 13

    WWW Awards
    « Reply #3 on: July 31, 2009, 08:19:09 AM »

    Thanks mate, i'm gona stick on these forums for a while know, best java forums yet, dw i will make this forum very active and busy.

    Thanks a lot  14 always good to see new people here, as for myself, i'm visiting the forum pretty much on a daily basis as well as many other memers but most of them are enjoying their summer right now.  clown
    Logged

    Java and .NET developer

    To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

    Fan of http://www.retardedweblogger.com
    Oh man, too much stuff to do in so little time.

    http://img222.imageshack....707/arkietomatoesmall.jpg
    Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
    Triple D, eerste Denken Dan Doen
    Javaforums.net :: a community about Java software development.
       

    Your Ad Here
     Logged
    Pages: [1]   Go Up
      Print  
     
    Jump to:  

    Your Ad Here
    Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
    TinyPortal v0.9.8 © Bloc
    Valid XHTML 1.0! Valid CSS!
    Page created in 0.331 seconds with 35 queries.

    Google visited last this page Yesterday at 08:07:00 PM