JTree and dropdown options on right clicking nodes

前端 未结 3 1255
醉话见心
醉话见心 2021-01-15 01:49

I\'m trying to use the JTree and implement different drop downs for all the parent nodes and the children nodes.

Here\'s what I\'ve done:

pmTree.addM         


        
3条回答
  •  耶瑟儿~
    2021-01-15 02:14

    You check the selected node:

    DefaultMutableTreeNode node = (DefaultMutableTreeNode)pmTree.getLastSelectedPathComponent();
    

    to see if you have a "parent" or a "child" node. You should select the node at the mouse position first, otherwise it will not be the right node. Call

    TreePath path = pmTree.getPathForLocation(evt.getX(), evt.getY());
    if (path != null) {
        pmTree.setSelectionPath(path);
    } else {
        return;
    }
    

    at the beginning of treePopup. (methods in Java should start with a lower case letter!)

提交回复
热议问题