import java.awt.Dimension; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.BoxLayout; import javax.swing.tree.TreePath; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; /* JTree的構造函數: JTree() JTree(Hashtable value) JTree(Object[] value)//只有這個構造函數能夠建立多個根結點 JTree(TreeModel newModel) JTree(TreeNode root) JTree(TreeNode root, boolean asksAllowsChildren) JTree(Vector value) */ public class JTreeDemo { public static void main (String[] args) { // 構造函數:JTree() JTree example1 = new JTree(); // 構造函數:JTree(Object[] value) Object[] letters = { " a " , " b " , " c " , " d " , " e " }; JTree example2 = new JTree (letters); // 構造函數:JTree(TreeNode root)(TreeNode空) // 用空結點建立樹 DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(); // 定義樹結點 JTree example3 = new JTree (node1); // 用此樹結點作參數調用 JTree的構造函數建立含有一個根結點的樹 // 構造函數:JTree(TreeNode root)(同上,只是TreeNode非空) // 用一個根結點建立樹 DefaultMutableTreeNode node2 = new DefaultMutableTreeNode( " Color " ); JTree example4 = new JTree (node2); // 結點不能夠顏色,默認爲白麪黑字 example4.setBackground (Color.lightGray); // 構造函數:JTree(TreeNode root, boolean asksAllowsChildren)(同上,只是TreeNode又有不一樣) // 使用DefaultMutableTreeNode類先用一個根結點建立樹,設置爲可添加孩子結點,再添加孩子結點 DefaultMutableTreeNode color = new DefaultMutableTreeNode( " Color " , true ); DefaultMutableTreeNode gray = new DefaultMutableTreeNode ( " Gray " ); color.add (gray); color.add ( new DefaultMutableTreeNode ( " Red " )); gray.add ( new DefaultMutableTreeNode ( " Lightgray " )); gray.add ( new DefaultMutableTreeNode ( " Darkgray " )); color.add ( new DefaultMutableTreeNode ( " Green " )); JTree example5 = new JTree (color); // 構造函數:JTree(TreeNode root)(同上,只是TreeNode非空) // 經過逐個添加結點建立樹 DefaultMutableTreeNode biology = new DefaultMutableTreeNode ( " Biology " ); DefaultMutableTreeNode animal = new DefaultMutableTreeNode ( " Animal " ); DefaultMutableTreeNode mammal = new DefaultMutableTreeNode ( " Mammal " ); DefaultMutableTreeNode horse = new DefaultMutableTreeNode ( " Horse " ); mammal.add (horse); animal.add (mammal); biology.add (animal); JTree example6 = new JTree (biology); horse.isLeaf(); horse.isRoot(); // 構造函數:JTree(TreeModel newModel) // 用DefaultMutableTreeNodel類定義一個結點再用這個結點作參數定義一個用DefaultTreeMode // 建立一個樹的模型,再用JTree的構造函數建立一個樹 DefaultMutableTreeNode root = new DefaultMutableTreeNode ( " Root1 " ); DefaultMutableTreeNode child1 = new DefaultMutableTreeNode ( " Child1 " ); DefaultMutableTreeNode child11 = new DefaultMutableTreeNode ( " Child11 " ); DefaultMutableTreeNode child111 = new DefaultMutableTreeNode ( " Child111 " ); root.add (child1); child1.add (child11); child11.add (child111); DefaultTreeModel model = new DefaultTreeModel (root); JTree example7 = new JTree (model); JPanel panel = new JPanel(); panel.setLayout ( new BoxLayout (panel, BoxLayout.X_AXIS)); panel.setPreferredSize ( new Dimension ( 700 , 400 )); panel.add ( new JScrollPane (example1)); // JTree必須放在JScrollPane上 panel.add ( new JScrollPane (example2)); panel.add ( new JScrollPane (example3)); panel.add ( new JScrollPane (example4)); panel.add ( new JScrollPane (example5)); panel.add ( new JScrollPane (example6)); panel.add ( new JScrollPane (example7)); JFrame frame = new JFrame ( " JTreeDemo " ); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setContentPane (panel); frame.pack(); frame.show(); } }
import java.awt.Dimension;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.BoxLayout;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/*
JTree的構造函數:
JTree()
JTree(Hashtable value)
JTree(Object[] value)//只有這個構造函數能夠建立多個根結點
JTree(TreeModel newModel)
JTree(TreeNode root)
JTree(TreeNode root, boolean asksAllowsChildren)
JTree(Vector value)
*/
public class JTreeDemo
{
public static void main (String[] args)
{
// 構造函數:JTree()
JTree example1 = new JTree();
// 構造函數:JTree(Object[] value)
Object[] letters = { " a " , " b " , " c " , " d " , " e " };
JTree example2 = new JTree (letters);
// 構造函數:JTree(TreeNode root)(TreeNode空)
// 用空結點建立樹
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(); // 定義樹結點
JTree example3 = new JTree (node1); // 用此樹結點作參數調用 JTree的構造函數建立含有一個根結點的樹
// 構造函數:JTree(TreeNode root)(同上,只是TreeNode非空)
// 用一個根結點建立樹
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode( " Color " );
JTree example4 = new JTree (node2); // 結點不能夠顏色,默認爲白麪黑字
example4.setBackground (Color.lightGray);
// 構造函數:JTree(TreeNode root, boolean asksAllowsChildren)(同上,只是TreeNode又有不一樣)
// 使用DefaultMutableTreeNode類先用一個根結點建立樹,設置爲可添加孩子結點,再添加孩子結點
DefaultMutableTreeNode color = new DefaultMutableTreeNode( " Color " , true );
DefaultMutableTreeNode gray = new DefaultMutableTreeNode ( " Gray " );
color.add (gray);
color.add ( new DefaultMutableTreeNode ( " Red " ));
gray.add ( new DefaultMutableTreeNode ( " Lightgray " ));
gray.add ( new DefaultMutableTreeNode ( " Darkgray " ));
color.add ( new DefaultMutableTreeNode ( " Green " ));
JTree example5 = new JTree (color);
// 構造函數:JTree(TreeNode root)(同上,只是TreeNode非空)
// 經過逐個添加結點建立樹
DefaultMutableTreeNode biology = new DefaultMutableTreeNode ( " Biology " );
DefaultMutableTreeNode animal = new DefaultMutableTreeNode ( " Animal " );
DefaultMutableTreeNode mammal = new DefaultMutableTreeNode ( " Mammal " );
DefaultMutableTreeNode horse = new DefaultMutableTreeNode ( " Horse " );
mammal.add (horse);
animal.add (mammal);
biology.add (animal);
JTree example6 = new JTree (biology);
horse.isLeaf();
horse.isRoot();
// 構造函數:JTree(TreeModel newModel)
// 用DefaultMutableTreeNodel類定義一個結點再用這個結點作參數定義一個用DefaultTreeMode
// 建立一個樹的模型,再用JTree的構造函數建立一個樹
DefaultMutableTreeNode root = new DefaultMutableTreeNode ( " Root1 " );
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode ( " Child1 " );
DefaultMutableTreeNode child11 = new DefaultMutableTreeNode ( " Child11 " );
DefaultMutableTreeNode child111 = new DefaultMutableTreeNode ( " Child111 " );
root.add (child1); child1.add (child11); child11.add (child111);
DefaultTreeModel model = new DefaultTreeModel (root);
JTree example7 = new JTree (model);
JPanel panel = new JPanel();
panel.setLayout ( new BoxLayout (panel, BoxLayout.X_AXIS));
panel.setPreferredSize ( new Dimension ( 700 , 400 ));
panel.add ( new JScrollPane (example1)); // JTree必須放在JScrollPane上
panel.add ( new JScrollPane (example2));
panel.add ( new JScrollPane (example3));
panel.add ( new JScrollPane (example4));
panel.add ( new JScrollPane (example5));
panel.add ( new JScrollPane (example6));
panel.add ( new JScrollPane (example7));
JFrame frame = new JFrame ( " JTreeDemo " );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panel);
frame.pack();
frame.show();
}
}java
××××××××××××××××××××××××××××××××××××××××××××××
node
在實際開發過程當中會常常使用JTree組件,平時會遇到這樣或那樣的問題,在此將偶得一點經驗寫下來,與你們共享,但願對你們有所幫助。數組
private JTree jtNetDevice;//數組件申明
private JScrollPane jspTree;//滾動面板申明jsp
一、初始化
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
jtNetDevice = new JTree(rootNode);
jtNetDevice.setAutoscrolls(true);
getTreeSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);//設置單選模式
jspTree = new JScrollPane();
jspTree.getViewport().add(jtNetDevice, null);函數
二、三個常常使用的取值函數
private DefaultTreeModel getTreeModel(){
return (DefaultTreeModel)jtNetDevice.getModel();
}spa
private DefaultMutableTreeNode getRootNode(){
return (DefaultMutableTreeNode)getTreeModel().getRoot();
}
private TreeSelectionModel getTreeSelectionModel(){
return jtNetDevice.getSelectionModel();
}
code
三、根據node獲得path:
TreePath visiblePath = new TreePath(getTreeModel().getPathToRoot(node));開發
四、根據Path展開到該節點
jtNetDevice.makeVisible(visiblePath);get
五、根據path設定該節點選定
jtNetDevice.setSelectionPath(visiblePath);it
六、選中節點的方法
首先,根據節點獲得樹路徑,其中chosen爲須要選中的節點
TreePath visiblePath = new TreePath( ( (DefaultTreeModel) jtNetDevice.getModel()).
getPathToRoot(chosen));
而後根據Path選中該節點
jtNetDevice.setSelectionPath(visiblePath);
七、滾動到可見位置
jtNetDevice.scrollPathToVisible(visiblePath);
八、給JTree添加右鍵彈出菜單
void jtNetDevice_mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());//彈出右鍵菜單
}
}
九、關於JTree的展開
// If expand is true, expands all nodes in the tree.
// Otherwise, collapses all nodes in the tree.
public void expandAll(JTree tree, boolean expand) {
TreeNode root = (TreeNode)tree.getModel().getRoot();
// Traverse tree from root
expandAll(tree, new TreePath(root), expand);
}
private void expandAll(JTree tree, TreePath parent, boolean expand) {
// Traverse children
TreeNode node = (TreeNode)parent.getLastPathComponent();
if (node.getChildCount() >= 0) {
for (Enumeration e=node.children(); e.hasMoreElements(); ) {
TreeNode n = (TreeNode)e.nextElement();
TreePath path = parent.pathByAddingChild(n);
expandAll(tree, path, expand);
}
}
// Expansion or collapse must be done bottom-up
if (expand) {
tree.expandPath(parent);
} else {
tree.collapsePath(parent);
}
}
十、如何遍歷JTree // 建立樹 JTree tree = new JTree(); // 添加樹節點...... // 遍歷全部節點 visitAllNodes(tree); // 僅遍歷展開的節點 visitAllExpandedNodes(tree); // Traverse all nodes in tree public void visitAllNodes(JTree tree) { TreeNode root = (TreeNode)tree.getModel().getRoot(); visitAllNodes(root); } public void visitAllNodes(TreeNode node) { // node is visited exactly once process(node); if (node.getChildCount() >= 0) { for (Enumeration e=node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode)e.nextElement(); visitAllNodes(n); } } } // Traverse all expanded nodes in tree public void visitAllExpandedNodes(JTree tree) { TreeNode root = (TreeNode)tree.getModel().getRoot(); visitAllExpandedNodes(tree, new TreePath(root)); } public void visitAllExpandedNodes(JTree tree, TreePath parent) { // Return if node is not expanded if (!tree.isVisible(parent)) { return; } // node is visible and is visited exactly once TreeNode node = (TreeNode)parent.getLastPathComponent(); process(node); // Visit all children if (node.getChildCount() >= 0) { for (Enumeration e=node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode)e.nextElement(); TreePath path = parent.pathByAddingChild(n); visitAllExpandedNodes(tree, path); } } }