6.3.1. Simple Trees

In our first example program, we will simply display a tree with a few nodes (see Figure 6.18 on p. 424). As with many other Swing components, you need to provide a model of the data, and the component displays it for you. To construct a JTree, supply the tree model in the constructor:

TreeModel model = . . .; JTree tree = new JTree(model);

Image Note

There are also constructors that construct trees out of a collection of elements:

JTree(Object[] nodes) JTree(Vector<?> nodes) JTree(Hashtable<?, ?> nodes) // the values become the nodes

These constructors are not very useful. They merely build a forest ...

Get Core Java® Volume II—Advanced Features, Ninth Edition now with O’Reilly online learning.

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