Adding editable nodes to JTree

This article explains how to create a JTree and add or insert a new node to it by clicking a button.

JTree cells can be made editable by using method setEditable(true). This will make the cell available for edition after clicking twice on it. Insertion of a node can be done by using the insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) method of the DefaultTreeModel.

Example Code

This example creates a editable JTree. The Add Node button will add a new node to the selected node at the end and will immediately make it available for editing.

Following JTree methods makes the newly added node visible and editable.

  • scrollPathToVisible(TreePath) : Scroll to the newly added node and makes it visible.
  • setSelectionPath(TreePath ) : Selects the newly added node.
  • startEditingAtPath(TreePath) : Initiates the editing for the node.
 * This example is from
 * for more information visit,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

 * This class creates a editable JTree and allows adding
 * new nodes to it, which are immediately made available for
 * editing after adding
 * @author Rahul Sapkal([email protected])
public class EditableTree extends JFrame implements ActionListener {
  private JTree m_tree;
  private DefaultTreeModel m_model;
  private JButton m_addButton;

   * Constructor
  public EditableTree() {
    setTitle("Editable JTree Demo");

    // constructing tree
    TreeNode rootNode = buildDummyTree();
    m_model = new DefaultTreeModel(rootNode);

    m_tree = new JTree(m_model);

    JScrollPane scrollPane = new JScrollPane(m_tree);
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    m_addButton = new JButton("Add Node");
    getContentPane().add(panel, BorderLayout.SOUTH);

    setSize(300, 400);

   * This method builds a dummy tree and returns the root node
   * @return root node
  public TreeNode buildDummyTree() {
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("JavaReference");
    DefaultMutableTreeNode forums = new DefaultMutableTreeNode("Forum");
    forums.add(new DefaultMutableTreeNode("Thread 1"));
    forums.add(new DefaultMutableTreeNode("Thread 2"));
    forums.add(new DefaultMutableTreeNode("Thread 3"));
    DefaultMutableTreeNode articles = new DefaultMutableTreeNode("Articles");
    articles.add(new DefaultMutableTreeNode("Article 1"));
    articles.add(new DefaultMutableTreeNode("Article 2"));
    DefaultMutableTreeNode examples = new DefaultMutableTreeNode("Examples");
    examples.add(new DefaultMutableTreeNode("Examples 1"));
    examples.add(new DefaultMutableTreeNode("Examples 2"));
    examples.add(new DefaultMutableTreeNode("Examples 3"));


    return rootNode;

   * Handles the button action
  public void actionPerformed(ActionEvent event) {
    if (event.getSource().equals(m_addButton)) {
      DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();

      if (selNode != null) {
        // add new node as a child of a selected node at the end
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node");
        m_model.insertNodeInto(newNode, selNode, selNode.getChildCount());

        //make the node visible by scroll to it
        TreeNode[] nodes = m_model.getPathToRoot(newNode);
        TreePath path = new TreePath(nodes);

        //select the newly added node

        //Make the newly added node editable

   * Main method to run as an Application
   * @param argv
  public static void main(String[] arg) {
    EditableTree editableTree = new EditableTree();

//End EditableTree


Below is an image of the demo. The New Node is added and made editable after clicking Add Node button. Double click on any node makes it editable.


Leave a Reply