A specific node in the tree can be identified by TreePath
(the object that encapsulates the node and all its ancestors) or by its display row, where each row in the display area displays a node. The expansion node is a non-leaf node (identified by a return of false TreeModel.isLeaf(node)
), and when all its ancestors are expanded , the node displays its child nodes. collapsing nodes are the nodes that hide them. A hidden node is a node that is located below the collapsed ancestor. The parent nodes of all viewable nodes can be expanded, but they can be displayed, or they may not be displayed. The display node is viewable and is located in a display area where it can be seen.
Construction Method Summary |
JTree() Return with the sample model JTree . |
JTree(Hashtable<?,?> value) Returns the Hashtable root from the created JTree , which does not show. |
JTree(Object[] value) Returns JTree , specifying each element of the array as a child node of the new root node that is not displayed. |
JTree(TreeModel newModel) Returns JTree An instance that displays the root node-creates a tree with the specified data model. |
JTree(TreeNode root) Returns JTree , specified TreeNode as its root, which displays the root node. |
JTree(TreeNode root, boolean asksAllowsChildren) Returns JTree , specified TreeNode as its root, displays the root node in the specified manner, and determines whether the node is a leaf node. |
JTree(Vector<?> value) Returns a JTree Vector child node that specifies each element as a new root node that is not displayed. |
JTree (TreeNode Root)
-
returns
JTree
, specified
TreeNode
as its root, which displays the root node. By default, a tree can define a leaf node as any node that does not have a ribbon node.
The following creates an instance of the public
jtree(TreeNode root) using the constructor method
-
?
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeDemo {
public static void main(String[] args) {
// 创建没有父节点和子节点、但允许有子节点的树节点,并使用指定的用户对象对它进行初始化。
// public DefaultMutableTreeNode(Object userObject)
DefaultMutableTreeNode node1 =
new DefaultMutableTreeNode(
"软件部"
);
node1.add(
new DefaultMutableTreeNode(
new User(
"小花"
)));
node1.add(
new DefaultMutableTreeNode(
new User(
"小虎"
)));
node1.add(
new DefaultMutableTreeNode(
new User(
"小龙"
)));
DefaultMutableTreeNode node2 =
new DefaultMutableTreeNode(
"销售部"
);
node2.add(
new DefaultMutableTreeNode(
new User(
"小叶"
)));
node2.add(
new DefaultMutableTreeNode(
new User(
"小雯"
)));
node2.add(
new DefaultMutableTreeNode(
new User(
"小夏"
)));
DefaultMutableTreeNode top =
new DefaultMutableTreeNode(
"职员管理"
);
top.add(
new DefaultMutableTreeNode(
new User(
"总经理"
)));
top.add(node1);
top.add(node2);
final JTree tree =
new JTree(top);
JFrame f =
new JFrame(
"JTreeDemo"
);
f.add(tree);
f.setSize(
300
,
300
);
f.setVisible(
true
);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加选择事件
tree.addTreeSelectionListener(
new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
if (node ==
null
)
return
;
Object object = node.getUserObject();
if (node.isLeaf()) {
User user = (User) object;
System.out.println(
"你选择了:" + user.toString());
}
}
});
}
}
class User {
private String name;
public User(String n) {
name = n;
}
// 重点在toString,节点的显示文本就是toString
public String toString() {
return name;
}
}
|
Http://www.cnblogs.com/taoweiji/archive/2013/02/08/2909214.html
How to use the Java Swing Tree component JTree (GO)