Tree Events

Trees generate three new types of events that are worth mentioning. Apart from the obvious selection and expansion events from the graphical side, you can also catch structural changes with model events.

The TreeModelEvent Class

The TreeModelEvent class encapsulates model changes in the form of the path that has changed, as well as information on the children of that path.


protected int[] childIndices, protected Object[] children

Hold the children and their indices in the parent node associated with this event.

protected TreePath path

Holds the path to the parent of the children associated with this event.


public TreeModelEvent(Object source, Object path[], int childIndices[], Object children[]), public TreeModelEvent(Object source, TreePath path, int childIndices[], Object children[])

These constructors allow you to build an event that encompasses the children of a modified node. This type of event is useful if the references to the node’s children have changed, or if the number of children has changed.

public TreeModelEvent(Object source, Object path[]), public TreeModelEvent(Object source TreePath path)

If the modified node is the only interesting node for this event (if its value changed, but nothing happened to its children, for example) then you can use these constructors.


public int[] getChildIndices(), public Object[] getChildren()

If the event contains information about the affected children of a node, you can retrieve those indices and the ...

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.