package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.util.Optional;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public final class MainPanel extends JPanel {
    private MainPanel() {
        super(new BorderLayout());
        JTree tree = new JTree();
        tree.setComponentPopupMenu(new TreePopupMenu());
        add(new JScrollPane(tree));
        setPreferredSize(new Dimension(320, 240));
    }
    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class TreePopupMenu extends JPopupMenu {
    protected TreePath path;
    private final Action addChildNodeAction = new AbstractAction("add child node") {
        @Override public void actionPerformed(ActionEvent e) {
            JTree tree = (JTree) getInvoker();
            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
            DefaultMutableTreeNode self  = (DefaultMutableTreeNode) path.getLastPathComponent();
            DefaultMutableTreeNode child = new DefaultMutableTreeNode("New child node");
            self.add(child);
            model.reload(self);
            //or: model.insertNodeInto(child, self, self.getChildCount());
        }
    };
    private final Action addAboveAction = new AbstractAction("insert preceding sibling node") {
        @Override public void actionPerformed(ActionEvent e) {
            JTree tree = (JTree) getInvoker();
            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
            MutableTreeNode self   = (MutableTreeNode) path.getLastPathComponent();
            MutableTreeNode parent = (MutableTreeNode) self.getParent();
            MutableTreeNode child  = new DefaultMutableTreeNode("New preceding sibling");
            int index = model.getIndexOfChild(parent, self);
            parent.insert(child, index);
            model.reload(parent);
            //or: model.insertNodeInto(child, parent, index);
        }
    };
    private final Action addBelowAction = new AbstractAction("insert following sibling node") {
        @Override public void actionPerformed(ActionEvent e) {
            JTree tree = (JTree) getInvoker();
            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
            MutableTreeNode self   = (MutableTreeNode) path.getLastPathComponent();
            MutableTreeNode parent = (MutableTreeNode) self.getParent();
            MutableTreeNode child  = new DefaultMutableTreeNode("New following sibling");
            int index = model.getIndexOfChild(parent, self);
            parent.insert(child, index + 1);
            model.reload(parent);
            //or: model.insertNodeInto(child, parent, index + 1);
        }
    };
    protected TreePopupMenu() {
        super();
        add(addChildNodeAction);
        addSeparator();
        add(addAboveAction);
        add(addBelowAction);
    }
    @Override public void show(Component c, int x, int y) {
        if (c instanceof JTree) {
            JTree tree = (JTree) c;
            path = tree.getPathForLocation(x, y);
            Optional.ofNullable(path).ifPresent(tp -> super.show(c, x, y));
        }
    }
}

@@ -1418,147 +1418,103 @@
   pr
-ivate final Action addChildNodeAction = new AbstractAction(%22add child node%22) %7B%0A        @Override public void actionPerformed(ActionEvent e)
+otected TreePopupMenu() %7B%0A        super();%0A        add(%22add child node%22).addActionListener(e -%3E
  %7B%0A 
@@ -1672,17 +1672,16 @@
 de self 
- 
 = (Defau
@@ -1884,32 +1884,33 @@
 ;%0A            //
+ 
 or: model.insert
@@ -1967,172 +1967,102 @@
    %7D
+);
 %0A    
-%7D;%0A
     
-private final Action addAboveAction = new AbstractAction(%22insert preceding sibling node%22) %7B%0A        @Override public void actionPerformed(ActionEvent e)
+addSeparator();%0A        add(%22insert preceding sibling node%22).addActionListener(e -%3E
  %7B%0A 
@@ -2205,34 +2205,32 @@
 leTreeNode self 
-  
 = (MutableTreeNo
@@ -2361,33 +2361,32 @@
 eTreeNode child 
- 
 = new DefaultMut
@@ -2566,32 +2566,33 @@
 ;%0A            //
+ 
 or: model.insert
@@ -2636,172 +2636,78 @@
    %7D
-%0A    %7D;%0A    private final Action addBelowAction = new AbstractAction(%22insert following sibling node%22) %7B%0A        @Override public void actionPerformed(ActionEvent e)
+);%0A        add(%22insert following sibling node%22).addActionListener(e -%3E
  %7B%0A 
@@ -2858,18 +2858,16 @@
 de self 
-  
 = (Mutab
@@ -3014,17 +3014,16 @@
 e child 
- 
 = new De
@@ -3223,16 +3223,17 @@
       //
+ 
 or: mode
@@ -3289,177 +3289,8 @@
    %7D
-%0A    %7D;%0A    protected TreePopupMenu() %7B%0A        super();%0A        add(addChildNodeAction);%0A        addSeparator();%0A        add(addAboveAction);%0A        add(addBelowAction
 );%0A 

use JPopupMenu#add(String) instead of JPopupMenu#add(Action)