public void actionPerformed(ActionEvent e){
  DefaultMutableTreeNode lastItem=getSelectedNode();
  DefaultMutableTreeNode parent;
  if (lastItem != null) {
    parent=(DefaultMutableTreeNode)lastItem.getParent();
    if (parent == null) {
      parent=(DefaultMutableTreeNode)treeModel.getRoot();
      lastItem=null;
    }
  }
 else {
    parent=(DefaultMutableTreeNode)treeModel.getRoot();
  }
  if (parent == null) {
    treeModel.setRoot(createNewNode("Inserted " + Integer.toString(insertCount++)));
  }
 else {
    int newIndex;
    if (lastItem == null) {
      newIndex=treeModel.getChildCount(parent);
    }
 else {
      newIndex=parent.getIndex(lastItem);
    }
    treeModel.insertNodeInto(createNewNode("Inserted " + Integer.toString(insertCount++)),parent,newIndex);
  }
}

public void actionPerformed ( ActionEvent e ) { DefaultMutableTreeNode lastItem = getSelectedNode ( ) ; DefaultMutableTreeNode parent ; if ( lastItem != null ) { parent = ( DefaultMutableTreeNode ) lastItem . getParent ( ) ; if ( parent == null ) { parent = ( DefaultMutableTreeNode ) treeModel . getRoot ( ) ; lastItem = null ; } } else { parent = ( DefaultMutableTreeNode ) treeModel . getRoot ( ) ; } if ( parent == null ) { treeModel . setRoot ( createNewNode ( "Inserted " + Integer . toString ( insertCount ++ ) ) ) ; } else { int newIndex ; if ( lastItem == null ) { newIndex = treeModel . getChildCount ( parent ) ; } else { newIndex = parent . getIndex ( lastItem ) ; } treeModel . insertNodeInto ( createNewNode ( "Inserted " + Integer . toString ( insertCount ++ ) ) , parent , newIndex ) ; } }
messaged when the user clicks on the insert menu item . determines the selection from the tree and inserts an item after that . if nothing is selected , an item is added to the root .
