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 javareference.com
 * for more information visit,
 * http://www.javareference.com
 */

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);
    m_tree.setEditable(true);
    m_tree.setSelectionRow(0);

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

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

    setSize(300, 400);
    setVisible(true);
  }

  /**
   * 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"));

    rootNode.add(forums);
    rootNode.add(articles);
    rootNode.add(examples);

    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);
        m_tree.scrollPathToVisible(path);

        //select the newly added node
        m_tree.setSelectionPath(path);

        //Make the newly added node editable
        m_tree.startEditingAtPath(path);
      }
    }
  }

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

//End EditableTree

Demonstration

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.

0

Leave a Reply