question: 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 ) ; } }
target:  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.