Java Reference
In-Depth Information
16.4.2
Class
TreeModel
Instances of this class define the abstract structure of the tree. This is done by
specifying the root and, for every node, specifying its children and their order. We
shall use the following methods.
DefaultTreeModel(TreeNode root)
int
getChildCount(TreeNode parent)
boolean
isLeaf(TreeNode node)
insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent,
int
k)
Object getChild(TreeNode parent,
int
k)
removeNodeFromParent(TreeNode child)
DefaultTreeModel(TreeNode root)
creates a tree model with root node
root
.
We may use a
DefaultMutableTreeNode
instead of
TreeNode
. There is no
parameterless constructor; you must always specify a root node!
int getChildCount(TreeNode parent)
returns the number of children of
par-
ent
.If
parent
has
n
children they are numbered 0
,
1
,...,
n
−
1.
boolean isLeaf(TreeNode node)
returns
true
if
node
is a leaf, i.e. has no chil-
dren, and
false
otherwise.
insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int
k)
makes
newChild
the
k
th child of
parent
.If
parent
has
n
children then
k
has to be one of 0
,
,
,...,
n
otherwise a runtime error will occur. If
k
is
strictly less than
n
then the former children
k
through
n
1
2
−
1 become children
k
+
1 through
n
and
newChild
is squeezed in between children
k
−
1 and
k
+
1.
Object getChild(TreeNode parent, int k)
returns the
k
th child of
parent
as
an
Object
and null if the
parent
does not have a
k
th child.
removeNodeFromParent(TreeNode child)
removes
child
from its parent. Note
that the whole subtree with root
child
is also removed.
16.4.3
Class
JTree
The graphical component for displaying trees in Swing is defined in the class
JTree
.Weonly need the following methods:
JTree(TreeModel treeModel)
setTreeModel(TreeModel treeModel)
putClientProperty("JTree.lineStyle","Angled");
JTree(TreeModel treeModel)
receives a
TreeModel
(or a
DefaultTreeModel
)
as parameter.
setTreeModel(TreeModel treeModel)
is used to set the tree model.
Search WWH ::
Custom Search