<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    聚合 管理  

    Blog Stats

    News

    我使用新博客啦:
    http://www.linjunhai.com/
    大家到我的新博客上看看吧!

    隨筆分類(28)

    文章分類(4)

    隨筆檔案(53)

    文章檔案(4)

    相冊

    相關鏈接


    林俊海的博客

    超級大菜鳥,每天要自強!

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

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

    程序代碼 程序代碼

    private JTree jtNetDevice;//數組件申明
    private JScrollPane jspTree;//滾動面板申明



    1、初始化
    程序代碼 程序代碼

        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);

    2、三個經常使用的取值函數
    程序代碼 程序代碼

      private DefaultTreeModel getTreeModel(){
        return (DefaultTreeModel)jtNetDevice.getModel();
      }

      private DefaultMutableTreeNode getRootNode(){
        return (DefaultMutableTreeNode)getTreeModel().getRoot();
      }
      
      private TreeSelectionModel getTreeSelectionModel(){
        return jtNetDevice.getSelectionModel();
      }

      

    3、根據node得到path:
    程序代碼 程序代碼

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


    4、根據Path展開到該節點
    程序代碼 程序代碼

      jtNetDevice.makeVisible(visiblePath);


    5、根據path設定該節點選定
    程序代碼 程序代碼

      jtNetDevice.setSelectionPath(visiblePath);


    6、選中節點的方法
      首先,根據節點得到樹路徑,其中chosen為需要選中的節點
    程序代碼 程序代碼

      TreePath visiblePath = new TreePath( ( (DefaultTreeModel) jtNetDevice.getModel()).
                                            getPathToRoot(chosen));

      然后根據Path選中該節點
    程序代碼 程序代碼

      jtNetDevice.setSelectionPath(visiblePath);


    7、滾動到可見位置
    程序代碼 程序代碼

      jtNetDevice.scrollPathToVisible(visiblePath);


    8、給JTree添加右鍵彈出菜單
    程序代碼 程序代碼

      void jtNetDevice_mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
          jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());//彈出右鍵菜單
        }
      }


    9、關于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);
           }
       }



    10、如何遍歷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);
               }
           }
       }

    文章來源:http://www.ialvin.cn/blog/default.asp?id=74
    posted on 2007-10-16 04:11 林俊海 閱讀(229) 評論(0)  編輯  收藏 所屬分類: JAVA天地
    主站蜘蛛池模板: 日本一区二区在线免费观看| 亚洲国产精品成人精品软件| 深夜a级毛片免费视频| 成熟女人牲交片免费观看视频| 91嫩草免费国产永久入口| 成年女性特黄午夜视频免费看| 国产亚洲精aa成人网站| 亚洲人成片在线观看| 成年黄网站色大免费全看| 亚洲视频在线精品| 人人公开免费超级碰碰碰视频 | 一级做a爰片久久毛片免费陪 | 国产精品美女午夜爽爽爽免费| 亚洲中文字幕无码专区| 羞羞视频免费网站在线看| 日韩免费精品视频| 亚洲欧美日韩综合久久久久| 国产精品免费电影| 一级做a免费视频观看网站| 国产亚洲精品观看91在线| 国产亚洲精彩视频| 久久亚洲中文字幕精品一区四| 亚洲色大成网站www久久九| 尤物永久免费AV无码网站| 一区二区免费在线观看| 亚洲一区二区三区偷拍女厕| 日韩精品无码一区二区三区免费| 亚洲综合区小说区激情区| 在线观看特色大片免费网站| 亚洲熟妇色自偷自拍另类| 国产好大好硬好爽免费不卡| 亚洲国产精品综合久久久| 午夜视频在线观看免费完整版| 亚洲中文字幕无码av在线| 国产福利免费观看| a毛片全部免费播放| 亚洲综合色婷婷在线观看| 久久久久国产亚洲AV麻豆| 4399好看日本在线电影免费| 亚洲视频免费观看| 日本免费的一级v一片|