熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JTree用法及JTree使用經驗總結

2013-11-23 18:47:26  來源: Java核心技術 

  import  javaawtDimension;

  import  javaawtColor;

  import  javaxswingJFrame;

  import  javaxswingJPanel;

  import  javaxswingJScrollPane;

  import  javaxswingJTree;

  import  javaxswingBoxLayout;

  import  javaxswingtreeTreePath;

  import  javaxswingtreeDefaultMutableTreeNode;

  import  javaxswingtreeDefaultTreeModel;

  /*

  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 example  =   new  JTree();

  // 構造函數JTree(Object[] value)

  Object[] letters =  { a   b   c   d   e };

  JTree example  =   new  JTree (letters);

  // 構造函數JTree(TreeNode root)(TreeNode空)

  // 用空結點創建樹

  DefaultMutableTreeNode node  =   new  DefaultMutableTreeNode(); // 定義樹結點

  JTree example  =   new  JTree (node); // 用此樹結點做參數調用 JTree的構造函數創建含有一個根結點的樹

  // 構造函數JTree(TreeNode root)(同上只是TreeNode非空)

  // 用一個根結點創建樹

  DefaultMutableTreeNode node  =   new  DefaultMutableTreeNode( Color );

  JTree example  =   new  JTree (node); // 結點不可以顏色默認為白面黑字

  examplesetBackground (ColorlightGray);

  // 構造函數JTree(TreeNode root boolean asksAllowsChildren)(同上只是TreeNode又有不同)

  // 使用DefaultMutableTreeNode類先用一個根結點創建樹設置為可添加孩子結點再添加孩子結點

  DefaultMutableTreeNode color  =   new  DefaultMutableTreeNode( Color   true );

  DefaultMutableTreeNode gray  =   new  DefaultMutableTreeNode ( Gray );

  coloradd (gray);

  coloradd ( new  DefaultMutableTreeNode ( Red ));

  grayadd ( new  DefaultMutableTreeNode ( Lightgray ));

  grayadd ( new  DefaultMutableTreeNode ( Darkgray ));

  coloradd ( new  DefaultMutableTreeNode ( Green ));

  JTree example  =   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 );

  mammaladd (horse);

  animaladd (mammal);

  biologyadd (animal);

  JTree example  =   new  JTree (biology);

  horseisLeaf();

  horseisRoot();

  // 構造函數:JTree(TreeModel newModel)

  // 用DefaultMutableTreeNodel類定義一個結點再用這個結點做參數定義一個用DefaultTreeMode

  // 創建一個樹的模型再用JTree的構造函數創建一個樹

  DefaultMutableTreeNode root  =   new  DefaultMutableTreeNode ( Root );

  DefaultMutableTreeNode child  =   new  DefaultMutableTreeNode ( Child );

  DefaultMutableTreeNode child  =   new  DefaultMutableTreeNode ( Child );

  DefaultMutableTreeNode child  =   new  DefaultMutableTreeNode ( Child );

  rootadd (child); childadd (child); childadd (child);

  DefaultTreeModel model  =   new  DefaultTreeModel (root);

  JTree example  =   new  JTree (model);

  JPanel panel  =   new  JPanel();

  panelsetLayout ( new  BoxLayout (panel BoxLayoutX_AXIS));

  panelsetPreferredSize ( new  Dimension (   ));

  paneladd ( new  JScrollPane (example)); // JTree必須放在JScrollPane上

  paneladd ( new  JScrollPane (example));

  paneladd ( new  JScrollPane (example));

  paneladd ( new  JScrollPane (example));

  paneladd ( new  JScrollPane (example));

  paneladd ( new  JScrollPane (example));

  paneladd ( new  JScrollPane (example));

  JFrame frame  =   new  JFrame ( JTreeDemo );

  framesetDefaultCloseOperation (JFrameEXIT_ON_CLOSE);

  framesetContentPane (panel);

  framepack();

  frameshow();

  }

  } ××××××××××××××××××××××××××××××××××××××××××××××

  在實際開發過程中會經常使用JTree組件平時會遇到這樣或那樣的問題在此將偶得一點經驗寫下來與大家共享希望對大家有所幫助

  private JTree jtNetDevice;//數組件申明

  private JScrollPane jspTree;//滾動面板申明

  初始化

  DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(root);

  jtNetDevice = new JTree(rootNode);

  jtNetDevicesetAutoscrolls(true);

  getTreeSelectionModel()setSelectionMode(TreeSelectionModelSINGLE_TREE_SELECTION);//設置單選模式

  jspTree = new JScrollPane();

  jspTreegetViewport()add(jtNetDevice null);

  三個經常使用的取值函數

  private DefaultTreeModel getTreeModel(){

  return (DefaultTreeModel)jtNetDevicegetModel();

  }

  private DefaultMutableTreeNode getRootNode(){

  return (DefaultMutableTreeNode)getTreeModel()getRoot();

  }

  private TreeSelectionModel getTreeSelectionModel(){

  return jtNetDevicegetSelectionModel();

  }

  根據node得到path

  TreePath visiblePath = new TreePath(getTreeModel()getPathToRoot(node));

  根據Path展開到該節點

  jtNetDevicemakeVisible(visiblePath);

  根據path設定該節點選定

  jtNetDevicesetSelectionPath(visiblePath);

  選中節點的方法

  首先根據節點得到樹路徑其中chosen為需要選中的節點

  TreePath visiblePath = new TreePath( ( (DefaultTreeModel) jtNetDevicegetModel())

  getPathToRoot(chosen));

  然後根據Path選中該節點

  jtNetDevicesetSelectionPath(visiblePath);

  滾動到可見位置

  jtNetDevicescrollPathToVisible(visiblePath);

  給JTree添加右鍵彈出菜單

  void jtNetDevice_mouseReleased(MouseEvent e) {

  if (eisPopupTrigger()) {

  jPopupMenushow(egetComponent() egetX() egetY());//彈出右鍵菜單

  }

  }

  關於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)treegetModel()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)parentgetLastPathComponent();

  if (nodegetChildCount() >= ) {

  for (Enumeration e=nodechildren(); ehasMoreElements(); ) {

  TreeNode n = (TreeNode)enextElement();

  TreePath path = parentpathByAddingChild(n);

  expandAll(tree path expand);

  }

  }

  // Expansion or collapse must be done bottomup

  if (expand) {

  treeexpandPath(parent);

  } else {

  llapsePath(parent);

  }

  }

  如何遍歷JTree

  // 創建樹

  JTree tree = new JTree();

  // 添加樹節點

  // 遍歷所有節點

  visitAllNodes(tree);

  // 僅遍歷展開的節點

  visitAllExpandedNodes(tree);

  // Traverse all nodes in tree

  public void visitAllNodes(JTree tree) {

  TreeNode root = (TreeNode)treegetModel()getRoot();

  visitAllNodes(root);

  }

  public void visitAllNodes(TreeNode node) {

  // node is visited exactly once

  process(node);

  if (nodegetChildCount() >= ) {

  for (Enumeration e=nodechildren(); ehasMoreElements(); ) {

  TreeNode n = (TreeNode)enextElement();

  visitAllNodes(n);

  }

  }

  }

  // Traverse all expanded nodes in tree

  public void visitAllExpandedNodes(JTree tree) {

  TreeNode root = (TreeNode)treegetModel()getRoot();

  visitAllExpandedNodes(tree new TreePath(root));

  }

  public void visitAllExpandedNodes(JTree tree TreePath parent) {

  // Return if node is not expanded

  if (!treeisVisible(parent)) {

  return;

  }

  // node is visible and is visited exactly once

  TreeNode node = (TreeNode)parentgetLastPathComponent();

  process(node);

  // Visit all children

  if (nodegetChildCount() >= ) {

  for (Enumeration e=nodechildren(); ehasMoreElements(); ) {

  TreeNode n = (TreeNode)enextElement();

  TreePath path = parentpathByAddingChild(n);

  visitAllExpandedNodes(tree path);

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25738.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.