聚合 管理  

          Blog Stats

          News

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

          隨筆分類(lèi)(28)

          文章分類(lèi)(4)

          隨筆檔案(53)

          文章檔案(4)

          相冊(cè)

          相關(guān)鏈接


          林俊海的博客

          超級(jí)大菜鳥(niǎo),每天要自強(qiáng)!

          在實(shí)際開(kāi)發(fā)過(guò)程中會(huì)經(jīng)常使用JTree組件,平時(shí)會(huì)遇到這樣或那樣的問(wèn)題,在此將偶得一點(diǎn)經(jīng)驗(yàn)寫(xiě)下來(lái),與大家共享,希望對(duì)大家有所幫助。
          ......

          在實(shí)際開(kāi)發(fā)過(guò)程中會(huì)經(jīng)常使用JTree組件,平時(shí)會(huì)遇到這樣或那樣的問(wèn)題,在此將偶得一點(diǎn)經(jīng)驗(yàn)寫(xiě)下來(lái),與大家共享,希望對(duì)大家有所幫助。

          程序代碼 程序代碼

          private JTree jtNetDevice;//數(shù)組件申明
          private JScrollPane jspTree;//滾動(dòng)面板申明



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

              DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
              jtNetDevice = new JTree(rootNode);
              jtNetDevice.setAutoscrolls(true);
              getTreeSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SelectION);//設(shè)置單選模式
              jspTree = new JScrollPane();
              jspTree.getViewport().add(jtNetDevice, null);

          2、三個(gè)經(jīng)常使用的取值函數(shù)
          程序代碼 程序代碼

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

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

            

          3、根據(jù)node得到path:
          程序代碼 程序代碼

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


          4、根據(jù)Path展開(kāi)到該節(jié)點(diǎn)
          程序代碼 程序代碼

            jtNetDevice.makeVisible(visiblePath);


          5、根據(jù)path設(shè)定該節(jié)點(diǎn)選定
          程序代碼 程序代碼

            jtNetDevice.setSelectionPath(visiblePath);


          6、選中節(jié)點(diǎn)的方法
            首先,根據(jù)節(jié)點(diǎn)得到樹(shù)路徑,其中chosen為需要選中的節(jié)點(diǎn)
          程序代碼 程序代碼

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

            然后根據(jù)Path選中該節(jié)點(diǎn)
          程序代碼 程序代碼

            jtNetDevice.setSelectionPath(visiblePath);


          7、滾動(dòng)到可見(jiàn)位置
          程序代碼 程序代碼

            jtNetDevice.scrollPathToVisible(visiblePath);


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

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


          9、關(guān)于JTree的展開(kāi)
          程序代碼 程序代碼

             // 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
             // 創(chuàng)建樹(shù)
          程序代碼 程序代碼

             JTree tree = new JTree();
            
             // 添加樹(shù)節(jié)點(diǎn)......
            
             // 遍歷所有節(jié)點(diǎn)

             visitAllNodes(tree);
            
             // 僅遍歷展開(kāi)的節(jié)點(diǎn)
             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);
                     }
                 }
             }

          文章來(lái)源:http://www.ialvin.cn/blog/default.asp?id=74
          posted on 2007-10-16 04:11 林俊海 閱讀(231) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JAVA天地
          主站蜘蛛池模板: 桓台县| 西平县| 普定县| 潞西市| 双峰县| 乌拉特后旗| 集贤县| 余姚市| 长沙县| 大足县| 汉阴县| 屯门区| 上杭县| 措勤县| 察隅县| 比如县| 柳江县| 鄱阳县| 富裕县| 通渭县| 北碚区| 鹤壁市| 阳泉市| 兴海县| 阳信县| 大连市| 梁平县| 鸡东县| 彭山县| 股票| 白玉县| 赤城县| 安吉县| 青岛市| 五台县| 永济市| 岢岚县| 安徽省| 文成县| 黎川县| 宝山区|