嘟嘟

  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 阅读(1483) 评论(1)  编辑  收藏 所属分类: java basic

Feedback

# re: Jtree 添加右记事件[未登录] 2009-02-18 15:05 Anna
太谢谢了哦  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: