1. Technology

A TreeExpansionListener Example Program


Java Code Listing

The example Java program below shows how to create a tree structure using the JTree class. The tree is created by building up a structure using DefaultMutableTreeNodes. It's placed in a JScrollPane and into a JFrame along with a JTextArea to create a simple GUI.

The JTree implements the TreeExpansionListener as an anonymous inner class. When the tree expansion events occur - when a node is expanded or collapsed - the event information is displayed using the JTextArea.

The article that goes with this Java code listing is How to Implement a TreeExpansionListener.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JScrollPane;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeExpansionEvent;

public class TreeExpansionListenerExample {
    JTree exTree;
    JTextArea feedbackArea;

    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
             public void run()
                 new TreeExpansionListenerExample();         
    public TreeExpansionListenerExample()
        JFrame guiFrame = new JFrame();
        //make sure the program exits when the frame closes
        guiFrame.setTitle("Creating a Table Example");
        //This will center the JFrame in the middle of the screen
        //Create the tree stucture using the DefaultMutableTreeNodes
        //The tree is a representation of a small part of the British Royal Family
        DefaultMutableTreeNode Royals = new DefaultMutableTreeNode("Queen Elizabeth - Prince Philip");
        DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Prince Charles");
        DefaultMutableTreeNode grandChild1 = new DefaultMutableTreeNode("Prince Harry");
        DefaultMutableTreeNode grandChild2 = new DefaultMutableTreeNode("Prince William");
        DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Princess Anne");
        DefaultMutableTreeNode grandChild3 = new DefaultMutableTreeNode("Peter Philips");
        DefaultMutableTreeNode greatgrandChild1 = new DefaultMutableTreeNode("Savannah Philips");
        DefaultMutableTreeNode greatgrandChild2 = new DefaultMutableTreeNode("Isla Philips");
        DefaultMutableTreeNode grandChild4 = new DefaultMutableTreeNode("Zara Philips");
        DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Prince Andrew");
        DefaultMutableTreeNode grandChild5 = new DefaultMutableTreeNode("Princess Beatrice");
        DefaultMutableTreeNode grandChild6 = new DefaultMutableTreeNode("Princess Eugenie");
        DefaultMutableTreeNode child4 = new DefaultMutableTreeNode("Prince Edward");
        DefaultMutableTreeNode grandChild7 = new DefaultMutableTreeNode("Lady Louise");
        DefaultMutableTreeNode grandChild8 = new DefaultMutableTreeNode("Viscount Severn");
        exTree = new JTree(Royals);
        JScrollPane treePane = new JScrollPane(exTree);
        //Add the TreeExpansionListener as an anonymous inner class.
        //The treecollapsed and treeExpanded just appened information
        //to the JTextArea
        exTree.addTreeExpansionListener(new TreeExpansionListener(){
            public void treeCollapsed(TreeExpansionEvent e)
                feedbackArea.append("Node collapsed at " + e.getPath() + "\n");
            public void treeExpanded(TreeExpansionEvent e)
                feedbackArea.append("Node expanded at " + e.getPath()+ "\n");
        feedbackArea = new JTextArea("Event Feedback..\n");
        JScrollPane textPane = new JScrollPane(feedbackArea);
        guiFrame.add(treePane, BorderLayout.NORTH);
        guiFrame.add(textPane, BorderLayout.CENTER);
  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Handling Events
  6. A TreeExpansionListener Example Program

©2014 About.com. All rights reserved.