我已经建成三级目录树了,我想问右击树接点时如何才能出现右键菜单.请大家帮帮忙,实在是没有一点头绪啊.

解决方案 »

  1.   

    //右键点击事件jPopupMenu = new JPopupMenu();
    JMenuItem item1 = new JMenuItem("添加子节点");
    JMenuItem item2 = new JMenuItem("编辑节点");
    JMenuItem item3 = new JMenuItem("删除节点");jPopupMenu.add(item1);
    jPopupMenu.add(item2);
    jPopupMenu.add(item3);item1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    addNode();
    }
    });
    tree.add(jPopupMenu);
    tree.addMouseListener(new MouseAdapter() {
    public void mouseReleased(MouseEvent er) {
    try {
    //是否右键单击
    if (er.getClickCount() == 1 && SwingUtilities.isRightMouseButton(er)) {
    TreePath path = tree.getPathForLocation(er.getX(), er.getY());
    if (path == null)
    return;
    tree.setSelectionPath(path);
    jPopupMenu.show(tree, er.getX(), er.getY());
    }else if (er.getClickCount() == 1) {
    TreePath path = tree.getPathForLocation(er.getX(), er.getY());
    if (path == null)
    return;
    if (tree.isExpanded(path)) {
    tree.collapsePath(path);
    }
    else {
    tree.expandPath(path);
    }
    return;
    }
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    });
      

  2.   

    谢谢你 :mituzhishi(向往未来)!