嘟嘟

            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 閱讀(1492) 評論(1)  編輯  收藏 所屬分類: java basic

          Feedback

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

          主站蜘蛛池模板: 鹤壁市| 临沭县| 托克逊县| 泗阳县| 沾益县| 宾川县| 儋州市| 韩城市| 读书| 齐河县| 虹口区| 三门县| 柘城县| 达州市| 北碚区| 绵阳市| 淮北市| 江城| 城固县| 东兴市| 呼玛县| 印江| 罗定市| 两当县| 邵武市| 连山| 平顶山市| 土默特右旗| 宜都市| 友谊县| 鄂温| 武鸣县| 鹿泉市| 南昌县| 宜昌市| 偃师市| 民县| 永州市| 齐河县| 阿鲁科尔沁旗| 万宁市|