Cover Page by Gary Cornell, Cay S. Horstmann

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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 ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required