import java
import java
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
/*
JTree的構造函數:
JTree()
JTree(Hashtable value)
JTree(Object[] value)//只有這個構造函數可以創建多個根結點
JTree(TreeModel newModel)
JTree(TreeNode root)
JTree(TreeNode root
JTree(Vector value)
*/
public class JTreeDemo
{
public static void main (String[] args)
{
// 構造函數
JTree example
// 構造函數
Object[] letters = {
JTree example
// 構造函數
// 用空結點創建樹
DefaultMutableTreeNode node
JTree example
// 構造函數
// 用一個根結點創建樹
DefaultMutableTreeNode node
JTree example
example
// 構造函數
// 使用DefaultMutableTreeNode類先用一個根結點創建樹
DefaultMutableTreeNode color = new DefaultMutableTreeNode(
DefaultMutableTreeNode gray = new DefaultMutableTreeNode (
color
color
gray
gray
color
JTree example
// 構造函數
// 通過逐個添加結點創建樹
DefaultMutableTreeNode biology = new DefaultMutableTreeNode (
DefaultMutableTreeNode animal = new DefaultMutableTreeNode (
DefaultMutableTreeNode mammal = new DefaultMutableTreeNode (
DefaultMutableTreeNode horse = new DefaultMutableTreeNode (
mammal
animal
biology
JTree example
horse
horse
// 構造函數:JTree(TreeModel newModel)
// 用DefaultMutableTreeNodel類定義一個結點再用這個結點做參數定義一個用DefaultTreeMode
// 創建一個樹的模型
DefaultMutableTreeNode root = new DefaultMutableTreeNode (
DefaultMutableTreeNode child
DefaultMutableTreeNode child
DefaultMutableTreeNode child
root
DefaultTreeModel model = new DefaultTreeModel (root);
JTree example
JPanel panel = new JPanel();
panel
panel
panel
panel
panel
panel
panel
panel
panel
JFrame frame = new JFrame (
frame
frame
frame
frame
}
} ××××××××××××××××××××××××××××××××××××××××××××××
在實際開發過程中會經常使用JTree組件
private JTree jtNetDevice;//數組件申明
private JScrollPane jspTree;//滾動面板申明
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
jtNetDevice = new JTree(rootNode);
jtNetDevice
getTreeSelectionModel()
jspTree = new JScrollPane();
jspTree
private DefaultTreeModel getTreeModel(){
return (DefaultTreeModel)jtNetDevice
}
private DefaultMutableTreeNode getRootNode(){
return (DefaultMutableTreeNode)getTreeModel()
}
private TreeSelectionModel getTreeSelectionModel(){
return jtNetDevice
}
TreePath visiblePath = new TreePath(getTreeModel()
jtNetDevice
jtNetDevice
首先
TreePath visiblePath = new TreePath( ( (DefaultTreeModel) jtNetDevice
getPathToRoot(chosen));
然後根據Path選中該節點
jtNetDevice
jtNetDevice
void jtNetDevice_mouseReleased(MouseEvent e) {
if (e
jPopupMenu
}
}
// If expand is true
// Otherwise
public void expandAll(JTree tree
TreeNode root = (TreeNode)tree
// Traverse tree from root
expandAll(tree
}
private void expandAll(JTree tree
// Traverse children
TreeNode node = (TreeNode)parent
if (node
for (Enumeration e=node
TreeNode n = (TreeNode)e
TreePath path = parent
expandAll(tree
}
}
// Expansion or collapse must be done bottom
if (expand) {
tree
} else {
llapsePath(parent);
}
}
// 創建樹
JTree tree = new JTree();
// 添加樹節點
// 遍歷所有節點
visitAllNodes(tree);
// 僅遍歷展開的節點
visitAllExpandedNodes(tree);
// Traverse all nodes in tree
public void visitAllNodes(JTree tree) {
TreeNode root = (TreeNode)tree
visitAllNodes(root);
}
public void visitAllNodes(TreeNode node) {
// node is visited exactly once
process(node);
if (node
for (Enumeration e=node
TreeNode n = (TreeNode)e
visitAllNodes(n);
}
}
}
// Traverse all expanded nodes in tree
public void visitAllExpandedNodes(JTree tree) {
TreeNode root = (TreeNode)tree
visitAllExpandedNodes(tree
}
public void visitAllExpandedNodes(JTree tree
// Return if node is not expanded
if (!tree
return;
}
// node is visible and is visited exactly once
TreeNode node = (TreeNode)parent
process(node);
// Visit all children
if (node
for (Enumeration e=node
TreeNode n = (TreeNode)e
TreePath path = parent
visitAllExpandedNodes(tree
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25738.html