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

JTree ActionListener Example 

                         

Introduction 

In this section, you will learn about JTree Action Listener and its implementations. 

Program Description:

The following program constructs a tree on the frame containing tree with root node and a command button ("Add"). Whenever you click the "Add" button, every time  the ActionListener is called to perform the action using actionPerformed() method. The actionPerformed() method adds  nodes in JTree. The insertNodeInto() Method  adds a  new child at specified location in JTree.

Here is the code of program:

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JTreeActionListener extends JFrame {
    JButton addButton;
    JTree tree;
    DefaultMutableTreeNode root;
    DefaultMutableTreeNode child;
  public static void main(String[] av) {
    new JTreeActionListener();
  }
  
  public JTreeActionListener(){
    super("JTreeActionListener");
    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    root = new DefaultMutableTreeNode("root");
    child = new DefaultMutableTreeNode("Colors");
    root.add(child);
    child.add(new DefaultMutableTreeNode("Red"));
    child.add(new DefaultMutableTreeNode("Blue"));
    child.add(new DefaultMutableTreeNode("Orange"));
    child.add(new DefaultMutableTreeNode("Green"));
    child.add(new DefaultMutableTreeNode("Yellow"));
    tree = new JTree(root);
    JScrollPane scroller = new JScrollPane(tree);
    c.add(BorderLayout.CENTER, scroller);
    c.add(BorderLayout.NORTH, addButton = new JButton("Add"));
    addButton.addActionListener(new ActionListener(){

      public void actionPerformed(ActionEvent e){
        child = new DefaultMutableTreeNode("Java Tutorial");
        child.add(new DefaultMutableTreeNode("Java.Example"));
        child.add(new DefaultMutableTreeNode("Java Awt"));
        child.add(new DefaultMutableTreeNode("Java String"));
        child.add(new DefaultMutableTreeNode("Java Swing"));
        child.add(new DefaultMutableTreeNode("Java News"));
        child.add(new DefaultMutableTreeNode("Java IO"));
     ((DefaultTreeModeltree.getModel()).insertNodeInto(child, root, 0);
    }
    });
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(300,300);
  }
}

Download this program.

Output of the program: 

 

                         

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

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

how to give hyperlink to each tree node

Posted by dheena on Monday, 08.6.07 @ 22:35pm | #22804

Latest Searches:
checkbox button enable
Refresh
pthread.h
database and netbeans
jpa spring
Photoshop Drawing 3D P
socket
writing data to file
compare two arraylist
set value to httpsessi
struts internationalis
procedure in mysql
Javascript Date and Ti
How to send sms to mob
Photoshop Photo Effect
java program that conv
Web Hosting Tutorials
Isertion Sort
login window
jdbc insert
user login servletsusi
request.setAttribute
static class
align checkboxlist in
calling a method from
WHERE Statement in SQL
Combattons la programm
Icon
substring
explanation
panelGrid
sample source code to
<c:forEach> in JSTL
Session in Servlet
Java and JSF database
synchronized
'string variable in sw
text color
persistent cookie
login screen swing
mysql-connector-java-3
how to execute update
java inheritance
how to pass same varia
pagination javascrip
JSP in error page
equilateral triangle p
iterator
Eclipse 3.4 EJB
Photoshop Abstracts Co
rich text
Heap Sort in Java
code to store a string
simple calculator prog
read space character f
java runtime environme
string programe in jav
add the ArrayList to
Create XML file from f
drill down using jfree
simple calculator
save JDBC Connection f
hiding frame in jsp
DOM parsers.
Introduction to Spring
jta
SQL in Privilleages
jQuery To Slide Effect
product of all even nu
sаÑ?¦аÑ??ing
Photoshop Effects Grun
chart in visual basic
display message in app
struts framework
creating portlets usin
insert delete update j
reading from csv file
struts2 select
join tables using SQL
jndi and java lookup
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.