Adding a Node to a JTree Component
import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreePath; public class Main { public static void main(String[] argv) throws Exception { JTree tree = new JTree(); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); TreePath path = tree.getSelectionPath(); MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent(); MutableTreeNode newNode = new DefaultMutableTreeNode("green"); model.insertNodeInto(newNode, node, node.getChildCount()); } }