♠ Posted by Unknown in Adv Java at 21:26
JTree Class
A
tree is a component that presents a hierarchical view of data. A user has the
ability to expand or collapse individual subtrees in this display. Trees are
implemented in Swing by the JTree class, which extends JComponent.
Some of its constructors are show here:
Some of its constructors are show here:
JTree(Hashtable ht)
JTree(Object obj[])
JTree(TreeNode tn);
JTree(Vector v)
The
first form creates a tree in which each element of the hash table ht is a child
node. Each element of the array obj is a child node in the second form. The
tree node tn is the root of the tree in the third form. Finally, the last form
used the elements of vector v as child nodes.
A
JTree object generates events when a node is expanded or collapsed. The
addTreeExpansionListener() and removeTreeExpansionListener() methods allow
listeners to register and unregister for these notifications. The signatures of
these methods are show here:
void
addTreeExpansionListener(TreeExpansionListener tel)
void removeTreeExpansionListener(TreeExpansionListener
tel)
The
TreePath class encapsulates information about a path to a particular node in a
tree. It provides several constructors and methods.
The
TreeNode interface declares methods that obtain information about a tree node.
For example, it is possible to obtain a reference to the parent node or an
enumeration of the child nodes. The MutableTreeNode interface extends TreeNode.
It declares methods that can insert and remove child nodes or change the parent
node.
The
DefaultMutableTreeNode class implements the MutableTreeNode interface. It
represents a node in a tree. One of its constructors is shown here:
DefaultMutableTreeNode(Object obj)
To
create a hierarchy of tree nodes, the add() method of DefaultMutableNode can be
used. Its signature is shown here:
void add(MutableTreeNode child)
Tree
expansion events are described by the class TreeExpansionEvent in the
javax.swing.event package. The getPath() method of this class returns a
TreePath object that describes the path to the changed code. Its signature is
shown here:
TreePath getPath()
The
TreeExpansionListener interface provides the following two methods:
void treeCollapsed(TreeExpansionEvent tee)
void treeExpanded(TreeExpansionEvent tee)
Here
are the steps that you should follow to use a tree in an applet:
- Create
a JTree object.
- Create
a JScrollPane object. (The arguments to the constructor specify the tree
and the policies for vertical and horizontal scroll bars.)
- Add
the tree to the scroll pane.
- add the scroll pane to the content pane of the applet.
Example:
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class JTreeExample extends JApplet
{
JTree tree;
JTextField jtf;
public void init()
{
Container contentPane =
getContentPane();
contentPane.setLayout(new
BorderLayout());
DefaultMutableTreeNode top =
new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a =
new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 =
new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 =
new DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode b =
new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 =
new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 =
new DefaultMutableTreeNode("B2");
b.add(b2);
tree =new JTree(top);
int v =
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h =
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new
JScrollPane(tree, v, h);
contentPane.add(jsp,
BorderLayout.CENTER);
jtf = new
JTextField("",20);
contentPane.add(jtf,
BorderLayout.SOUTH);
tree.addMouseListener(new
MouseAdapter(){
public void
mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
});
}
void
doMouseClicked(MouseEvent me)
{
TreePath tp =
tree.getPathForLocation(me.getX(),me.getY());
if(tp != null)
{
jtf.setText(tp.toString());
}
else
{
jtf.setText("");
}
}
}
0 comments:
Post a Comment