By Tony Bevis on Friday, 20 October 2023
Category: Java

Java programming course: 14.13 JTree

JTree

The JTree class displays items which are structured in a hierarchy. Each item is known as a node, and each node can contain other nodes as its "children". Like JList and JTable, the JTree uses an associated model object (a TreeModel) to provide its data. However, in many cases it is easier to make use of the convenience class DefaultMutableTreeNode to represent each node in the tree. Suppose you want to model some of the countries of the five continents and have them displayed in a tree:

You will need to import package javax.swing.tree

Run the application to see the following display:

You can expand or contract any node by clicking the small "handle" graphic to the left of any parent item. If you expand Europe and then UK the screen should look as follows:

Next lesson: 14.14 JSplitPane

Related Posts

Leave Comments