The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners

How to Write a Tree Expansion Listener

Sometimes when using a tree(in the Creating a GUI with JFC/Swing trail), you might need to react when a branch becomes expanded or collapsed. For example, you might need to load or save data. Or you might need to prevent the user from expanding a particular node.

Two kinds of listeners report expansion and collapse occurrences: tree expansion listeners and tree-will-expand listeners. This page discusses the former. A tree expansion listener detects when an expansion or collapse has happened. In general, you should implement a tree expansion listener unless you might need to prevent an expansion or collapse from happening.

Tree-will-expand listeners are discussed in How to Write a Tree-Will-Expand Listener. A tree-will-expand listener detects when an expansion or collapse is about to happen.

The following applet demonstrates a simple tree expansion listener. The text area at the bottom of the applet displays a message every time a tree expansion event occurs. It's a straightforward, boring applet. To see a more interesting version that can veto expansions, go to How to Write a Tree-Will-Expand Listener.

Click this figure to run the applet.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.

The following code shows how the program handles expansion events. You can find all the applet's source code in TreeExpandEventDemo.java(in a .java source file).

public class TreeExpandEventDemo ... {
    ...
    void saySomething(String eventDescription, TreeExpansionEvent e) {
        textArea.append(eventDescription + "; "
                        + "path = " + e.getPath()
                        + newline);
    }

    class DemoArea ... implements TreeExpansionListener {
        ...
        public DemoArea() {
            ...
            tree.addTreeExpansionListener(this);
            ...
        }
        ...
        // Required by TreeExpansionListener interface.
        public void treeExpanded(TreeExpansionEvent e) {
            saySomething("Tree-expanded event detected", e);
        }

        // Required by TreeExpansionListener interface.
        public void treeCollapsed(TreeExpansionEvent e) {
            saySomething("Tree-collapsed event detected", e);
        }
    }
}

The Tree Expansion Event API

The TreeExpansionListener(in the API reference documentation) interface contains two methods:
void treeCollapsed(TreeExpansionEvent)
Called just after a tree node collapses.
void treeExpanded(TreeExpansionEvent)
Called just after a tree node expands.
Both methods have a single parameter: a TreeExpansionEvent(in the API reference documentation) object. The TreeExpansionEvent class defines the following method:
TreePath getPath()
Returns a TreePath(in the API reference documentation) object that identifies each node from the root of the tree to the collapsed/expanded node, inclusive.
Another useful method is getSource, which TreeExpansionEvent inherits from EventObject(in the API reference documentation).

Examples that Use Tree Expansion Listeners

The following table lists the examples that use tree expansion listeners.

Example Where Described Notes
TreeExpandEventDemo This section Displays a message whenever a tree expansion event occurs.
TreeExpandEventDemo2 How to Write a Tree-Will-Expand Listener Adds a tree-will-expand listener to TreeExpandEventDemo.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.