嘟嘟

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            26 Posts :: 0 Stories :: 6 Comments :: 0 Trackbacks
          先建一個菜單對象,處理JTree控件鼠標事件,然后將菜單顯示出來
          JPopupMenu popup = new JPopupMenu();
          JMenuItem modify = new JMenuItem("modify");
           modify.setActionCommand("modify");
           modify.addActionListener(this);
           popup.add(modify);

          1: 左鍵選中然后再右鍵去操作
          tree.addMouseListener(new MouseAdapter() {
             public void mousePressed(MouseEvent e) {
              if (e.getButton() == e.BUTTON3) {  //BUTTON3是鼠標右鍵
                DefaultMutableTreeNode node =  (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                pmn.show(e.getComponent(),e.getX(),e.getY());
              }
             }
            });

          2: 直接右擊
          public void jTree1_mousePressed(MouseEvent e) {
              TreePath tp=tree.getPathForLocation(e.getX(),e.getY());
             if (tp != null) {
                    tree.setSelectionPath(tp);
                    DefaultMutableTreeNode node =  (DefaultMutableTreeNode)tp.getLastPathComponent()
             } 
             pmn.show(e.getComponent(),e.getX(),e.getY());
              }
             }
            });
          posted on 2007-05-08 03:14 fyp1210 閱讀(1493) 評論(1)  編輯  收藏 所屬分類: java basic

          Feedback

          # re: Jtree 添加右記事件[未登錄] 2009-02-18 15:05 Anna
          太謝謝了哦  回復  更多評論
            

          主站蜘蛛池模板: 吉安市| 射洪县| 布拖县| 南郑县| 库尔勒市| 博客| 辽阳市| 慈利县| 新密市| 聂拉木县| 井冈山市| 大理市| 新巴尔虎左旗| 布拖县| 宕昌县| 城固县| 青海省| 平阳县| 增城市| 嘉善县| 翁牛特旗| 大洼县| 府谷县| 驻马店市| 沛县| 大姚县| 衡山县| 翼城县| 扎赉特旗| 巨野县| 抚顺县| 富民县| 娱乐| 建平县| 勐海县| 车险| 阳谷县| 汉阴县| 沙湾县| 东城区| 上饶市|