Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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:

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

It is very useful to have a reference..It really help us as a beginner...
Thank you and more power..
I hope you will help more....
Salamt sa inyo lahat.....

Posted by adrian on Monday, 09.29.08 @ 11:18am | #80763

i like your web site. its will be more useful for the beginners. in future i will get lot of information about java in roseindia.net.

Posted by alagarsamy on Friday, 05.18.07 @ 15:31pm | #16374

Latest Searches:
Ñ?Ñ?Ñ?Ñ?Ñ?
portlet spring
usebean tag in jsp
book store application
bean header
jsp for
date function java scr
make effect on pictuer
Java Pass Value
Photoshop Textures and
AddressAction
code for transpose of
Tree Grid using flex
csv file format
a simple program of jd
xml marshling
char at
creating a file
Selection Sort In Java
transpose of a matrix
sort user input number
marshling
smartclient
interface
retrieve image from da
substring
write to text file
Flash Math Physics Fla
EXT grid
MASHLING
previous
Struts simple example
swing close button
assign flex label text
jsp MENU SELECT CREATE
Thread Sleep Method
validate radio button
j2me insert image form
sequence number genera
10 mintiues guid to an
survey js
dojo debug
length of string
activate
daouble in java
Convert Time to Second
Call the setLocation
Buble Sort In Java
objective c
write to text file lin
modity
list iteration in java
source code of sale
applet in dojotype
Scala Plugin
using JButton
for loop tutorial
how to user JSpinner t
Gimp Tutorials
java random numbers
composite key in mysql
multiplication of numb
facesmessage
combobox jsp
struts spring
get single character u
Photoshop Christmas Ba
what is the system.out
Photoshop Brushes Spar
log
Matrix
java code for id gener
components in JSP
html:checkbox is check
hibernate with logj4 l
create a directory and
http servlet
container managed pers
program in java to ins
my Sql server through
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.