JTree, DefaultMutableTreeNode, TreeExpansionEvent class in javax.swing: constructors, methods

JTree class: JTree class is used to wrap a tree based control in javax.swing. A tree orders data in a hierarchical fashion. Sub trees can be expanded and collapsed by the user.

Types of constructors defined by JTree class:

To create a tree with elements of hash table as nodes.

JTree(Hashtable ht)

To create a tree with objects as nodes.

JTree(Object ob[ ])

To create a tree with tree node as root of tree.

JTree(TreeNode tn)

To create a tree with elements of vector as nodes.

JTree(Vector v)

Methods defined by JTree class:

getPathForLocation( ) method: It is used to translate a mouse click on a point of tree to a tree path.


TreePath getPathForLocation(int x, int y)

Where, x and y are coordinates of mouse click.

DefaultMutableTreeNode class: It is used to represent a node in a tree.

Constructor defined by DefaultMutableTreeNode class:

DefaultMutableTreeNode(Object ob)

Methods defined by DefaultMutableTreeNode class:

add( ) method: It is used to create tree node hierarchy.


void add(MutableTreeNode child)

TreeExpansionEvent class: It is used for wrapping tree expansion events.

Method defined by TreeExpansionEvent class:

getPath( ) method: It is used to return path to changed node.


TreePath getPath( )

Procedure to use a tree in an applet:

1) Create a JTree object.

2) Create a JScrollPane object.

3) Add tree to scroll pane.

4) Add scroll pane to content pane of applet.

Leave a Reply