简体   繁体   English

如何仅在单击弹出菜单项后重命名 JTree 的节点?

[英]How to rename the node of a JTree only after clicking pop-up menu item?

I want to rename the selected node after right-clicking there will be a popup menu containing rename and delete button which will do rename and delete operation after a single click (action listener).我想在右键单击后重命名所选节点,将出现一个包含重命名和删除按钮的弹出菜单,单击后将执行重命名和删除操作(动作侦听器)。 And also this popup menu should not be displayed after clicking on any parent node means it must be leaf-node.而且在点击任何父节点后不应显示此弹出菜单意味着它必须是叶节点。

这是带有“重命名”项目的弹出菜单的 Jtree

node.setUserObject(newName);
panelOfTheTree.repaint();
panelOfTheTree.revalidate();

Regarding the constraint of only being displayed on leaf nodes there are several ways to do this.关于只显示在叶节点上的约束,有几种方法可以做到这一点。 One way is to implement a MouseListener on the jtree:一种方法是在 jtree 上实现 MouseListener:

tree.addMouseListener(new MouseListener(){
   //...
   @Override
   public void MouseClicked(MouseEvent e){
       if(e.getButton() == MouseEvent.BUTTON3){ //right click
          //get node
          TreePath destinationPath = tree.getClosestPathForLocation(e.getX(), e.getY());
          DefaultMutableTreeNode nodeClicked = (DefaultMutableTreeNode)destinationPath.getLastPathComponent();
          if(nodeClicked .isLeaf()){
               //... build jpopupmenu
          }
               
       }
   }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM