JTree Remove Node

In this section, you will learn how to remove a node from the JTree component. Removing a node from JTree it means delete a node from the JTree component to individually and delete the root node directly.

JTree Remove Node

In this section, you will learn how to remove a node from the JTree component. Removing a node from JTree it means delete a node from the JTree component to individually and delete the root node directly.

JTree Remove Node

Removing a Node from the JTree Component

     

In this section, you will learn how to remove a node from the JTree component. Removing a node from JTree it means delete a node from the JTree component to individually and delete the root node directly. 

Description of program:

The following program helps you in removing or deleting root and child of root node from the JTree component. When this program runs, it will provide a graphical layout that has tree and two command button: "Remove Specific Node" and "Remove Root Node". If you want to delete an individual node from the JTree, you will click the "Remove Specific Node" button. After clicking this command button appears an input box that takes the name of node that have to be deleted from the JTree. If this input box is empty or blank, it will display a message "Node could not be deleted from tree" in message box and any node is not deleted from JTree. Again, you click the Remove Specific Node" button, it will take node name (New Volume (E:)) in the input box and click the "OK" command button. The given node is deleted from the JTree and it will display a message "Node are deleted from tree" in message box. And if you want to delete the root from JTree, you will click the "Remove Root Node" button, the root is deleted from the JTree and shows a message "Remove the root" in message box and both buttons are disable.

Description of code:

removeNodeFromParent(MutableTreeNode mNode):
This is the method that removes a node from its parent. 

Here is the code of program:

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

public class RemoveNodes{
public static JButton button1, button2;
public static DefaultTreeModel model;
public static TreePath path;
public static JTree tree;
public static DefaultMutableTreeNode nNode;
public static MutableTreeNode mNode;
String nodeName;
public static void main(String[] args) {
JFrame frame = new JFrame("Removing a Node to a JTree Component!");
JPanel panel = new JPanel();
DefaultMutableTreeNode myComputer = new DefaultMutableTreeNode("My Computer");
 DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
  DefaultMutableTreeNode vinod = new DefaultMutableTreeNode("Vinod");
  DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
  DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
  DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
  DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
  c.add(vinod);
  vinod.add(swing);
  swing.add(tr);
  myComputer.add(c);
  myComputer.add(a);
  myComputer.add(e);
  tree = new JTree(myComputer);
  panel.add(tree);
  button1 = new JButton("Remove Specific Node");
  button1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae){
  model = (DefaultTreeModel)tree.getModel();
  String nodeName = JOptionPane.showInputDialog(null, "Enter the node name 
  which have to be deleted from tree:"
);
  if(nodeName.equals("")){
  JOptionPane.showMessageDialog(null, "Node could not be 
  deleted from tree!"
);
  }
  else{
  path = tree.getNextMatch(nodeName, 0, Position.Bias.Forward);
  mNode = (MutableTreeNode)path.getLastPathComponent();
  model.removeNodeFromParent(mNode);
  JOptionPane.showMessageDialog(null, "Node are deleted from tree!");
  }
  }
  });
  panel.add(button1);
  button2 = new JButton("Remove Root Node");
  button2.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae){
  model = (DefaultTreeModel)tree.getModel();
  model.setRoot(null);
  button1.setEnabled(false);
  button2.setEnabled(false);
  JOptionPane.showMessageDialog(null, "Remove the root!");
  }
  });
  panel.add(button2);
  frame.add(panel);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setSize(200,300);
  frame.setVisible(true);
  }
}

Download this example.

Output of program:

Before removing a node:

Click the "Remove Specific Node" command button:

Click the "Ok" command button:

Again click the "OK" button:

Again click the "Remove Specific Node" command button:

Click the "OK" command button:

Again click the "OK" command button and get removing node from the tree:

Click the "Remove Root Node" command button:

Removing root: