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.*;
|
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:
![]() |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: JTree Remove Node View All Comments
Post your Comment