By: Nicholas Duchon.You can use the following
classes to create a fairly simple tree. In the example I did in an
earlier semester, I read in the entire data file, then passed a
handle to the structure to create the tree. The example uses the
following classes to create and display the tree:

javax.swing.tree.DefaultMutableTreeNode

javax.swing.JTree

The traversal of the data file should be the
same as the tree structure you wish to create.

Here's an example of the sort of code I think will
work nicely. In this case, we had the following classes:

Furniture (furs)

Manufacturer (f.mf)

Style (f.style)

Type (f.type)

The data structure in this case was an array of
Furniture, so it could be sorted very nicely and we just had to
step through the array in order to create the tree.

The tree items were arranged according to the
following levels: Manufacturer --> Style --> Type -->
Furniture item

The code has the concepts of:

current furniture (f)

current manufacturer (m)

current manufacturer node (d)

current type node (t)

current style node (s)

When one of those changes, a new node at that
level is created in the tree.

Code for Example 2:

// File:
JTreeExample2
// Date: Jul 19, 2013, Oct 3, 2018
// Author: Nicholas Duchon
// Purpose: a quick demonstration of populating and presenting a
JTree
// createDataTree creates a tree structure directly
as a JTree using DefaultMutableTreeNode's as elements of the
tree
// the DMTN references Element's, which are really
any Object you wish to define.
// Display - this code shows how to display the
results of various traversals also.
// Also added JSplitPane's to present traversals in
GUI's
// Notes -
// DMTN only has one set of children at each node,
so if your project is more complex, you will need to fix this
somehow.