JTree:
A tree is a component that presents a hierarchical view of data. The user has the ability to expand or collapse individual subtrees in this display.Trees are implemented in Swing by the JTree class. JTree generally uses the following constructor:
JTree(Object obj[]) JTree(Vector v) JTree(TreeNode tn)In the first form, the tree is constructed from the elements in the array obj.
The second form constructs the tree from the elements of vector v.
In the third form, the tree whose root node is specified by tn specifies the tree.
Although JTree is packaged in javax.swing, its support classes and interfaces are packaged in javax.swing.tree.
JTree relies on two models: TreeModel and TreeSelectionModel. JTree generates variety of events, but three relates specifically to trees: TreeExpansionEvent, TreeSelectionEvent, and TreeModelEvent.
TreeExpansionEvent events occur when a node is expanded or collapsed. TreeSelectionEvent is generated when the user selects or deselects a node within the tree. TreeModelEvent is fired when the data or structure of the tree changes.The isteners for these events are TreeExpansionListener, TreeSelectionListener and TreeModelListener respectively.
The tree event classes and listener interfaces are packaged in javax.swing.event. We can obtain the path to the selected object by calling getPath(), shown below:
TreePath getPath()
The MutableTreeNode interface extends TreeNode. It declares methods that can insert and remove child nodes or changes the parent node.
To create a hierarchy of tree nodes, the add() method of DefaultMutableTreeNode can be used as shown here:
void add(MutableTreeNode child)Here, child is a mutable tree node that is to be added as a child to the current node.
JTree does not provide any scrolling capabilities of its own. Instead, a JTree is typically placed within a JScrollPane.
Here are the steps to follow to use a tree:
1. Create an instance of JTree. 2. Create a JScrollPane and specify the tree as the object to be scrolled. 3. Add the tree to the scroll pane. 4. Add the scroll pane to the content pane.Example:
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; public class MyTree extends JApplet { JTree t; JLabel l1; String lang[]={"C","C++","JAVA","PYTHON","AJAX","PHP"}; public void init() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { makeGUI(); } }); } catch(Exception e){ System.out.println("Sorry some error occured "+e); } } private void makeGUI() { DefaultMutableTreeNode top=new DefaultMutableTreeNode("Language"); DefaultMutableTreeNode a=new DefaultMutableTreeNode("Programming"); top.add(a); DefaultMutableTreeNode a1=new DefaultMutableTreeNode("C"); a.add(a1); DefaultMutableTreeNode a2=new DefaultMutableTreeNode("Java"); a.add(a2); DefaultMutableTreeNode b=new DefaultMutableTreeNode("Web Based"); top.add(b); DefaultMutableTreeNode b1=new DefaultMutableTreeNode("PHP"); b.add(b1); DefaultMutableTreeNode b2=new DefaultMutableTreeNode("JSP"); b.add(b2); t=new JTree(top); JScrollPane scr=new JScrollPane(t); add(scr); setLayout(new FlowLayout()); l1=new JLabel("Choose a language"); add(l1,BorderLayout.SOUTH); t.addTreeSelectionListener(new TreeSelectionListener () { public void valueChanged(TreeSelectionEvent ae) { l1.setText(ae.getPath()+" is selected"); } }); } }Output:
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.