The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.tree  [15 examples] > Nodes  [6 examples]

e1025. Adding a Node to a JTree Component

    // Create tree
    JTree tree = new JTree();
    DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
    
    // Find node to which new node is to be added
    int startRow = 0;
    String prefix = "J";
    TreePath path = tree.getNextMatch(prefix, startRow, Position.Bias.Forward);
    MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();
    
    // Create new node
    MutableTreeNode newNode = new DefaultMutableTreeNode("green");
    
    // Insert new node as last child of node
    model.insertNodeInto(newNode, node, node.getChildCount());

 Related Examples
e1023. Visiting All the Nodes in a JTree Component
e1024. Finding a Node in a JTree Component
e1026. Removing a Node to a JTree Component
e1027. Converting a Node in a JTree Component to a TreePath
e1028. Converting All Nodes in a JTree Component to a TreePath Array

See also: Events    Layout    Node Expansion    Selections   


© 2002 Addison-Wesley.