Table of Contents- Using Trees
- Building a Tree
- Creating a Jtree component
- Making it select
- Putting it all togther
Using TreesA 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 TreeBefore 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]
GeSHi (java):
DefultMutableTreeNode
Created by GeSHI 1.0.7.20
class.
The
GeSHi (java):
DefultMutableTreeNode
Created by GeSHI 1.0.7.20
class bassically implements the TreeNode interface (simple aye:))
so whoopie now you can use
GeSHi (java):
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*
CostructerGeSHi (java):
DefultMutableTreeNode () - This creates an EMPTY tree node or a branch Lol, call it what u want.
Created by GeSHI 1.0.7.20
GeSHi (java):
DefultMutableTreeNode - This creates a tree node with a spersifc user object..
Created by GeSHI 1.0.7.20
MethodGeSHi (java):
void add
(TreeNode child
) -
This adds a child node
Created by GeSHI 1.0.7.20
GeSHi (java):
TreeNode getFirstChild
() - Gets the first of
this type of nodes child
Created by GeSHI 1.0.7.20
GeSHi (java):
DefultMutableTreeNode - Gets the next sibling duh..
getNextSibling()
Created by GeSHI 1.0.7.20
GeSHi (java):
TreeNode getParent
() - gets the nodes parent
Created by GeSHI 1.0.7.20
GeSHi (java):
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:
GeSHi (java):
DefultMutableTreeModel Ikram, BOT1, BOT2,
Happy, Sad, Cranky;
Created by GeSHI 1.0.7.20
Next you will have to create the RootNode:
GeSHi (java):
DefultMutableTreeModel root=
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