Home Java Example Java Swing JTree ActionListener Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JTree ActionListener Example

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

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: 

 

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 30, 2007

Ask Questions?    Discuss: JTree ActionListener Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com