tree.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e){
      if (e.getButton() == MouseEvent.BUTTON3) {
      //此函数得到距鼠标点击最近处的节点行数
      int row = tree.getClosestRowForLocation(e.getX(), e.getY());
      //此函数得到距鼠标点击最近处的节点路径
      TreePath treePath = 
      tree.getClosestPathForLocation(e.getX(), e.getY());
      //判断鼠标点击处的坐标是否在所选节点的Rectangle之内
      if (tree.getPathBounds(treePath).contains(e.getX(), e.getY())){
      //设置所选节点为当前节点
      tree.setSelectionRow(row);
      DefaultMutableTreeNode node =
              (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
      if (node == null) return;
      //判断该节点是否是叶子节点
      if (node.isLeaf()){
      //弹出对话框
      popupMenu.show(e.getComponent(),e.getX(),e.getY());
      }
      }
      }
      }
      });//that's ok!