Basic tree concepts

dbSwing has two tree components, JdbTree and JdbNavTree. The difference between them is the same as between any other dbSwing component and its "nav" equivalent: In other respects, the two tree components behave alike. Our help viewer uses a JdbNavTree, but this introduction applies to both tree components.

Data-awareness: A data-aware JdbTree works like a JdbList: it displays the column value of the current row in a data set, and it provides a restricted list of values that can be written to the column.

The difference between a JdbTree and a JdbList is that the tree is hierarchical. It has exactly one root node, and every node except the root is the child of another node. So every node is connected directly or indirectly to the root. The placement of one node as a child of another indicates a relationship between the two nodes. Items in a list don't have this kind of relationship. A list is like a tree that's only one level deep.

Selection: "Data-aware" means that the tree is synchronized with its data set. When you navigate the data set, the selection in the tree moves to the node whose value matches the value in the column that the tree is bound to. When you select a new node in the tree, one of two things happens:

Note that JdbTree and JdbNavTree consider both non-terminal nodes (those with child nodes under them) and terminal nodes to be selectable data items. You may or may not want this. It's helpful in this sample, where both non-terminal and terminal nodes represent HTML pages. But if a tree represents a file system, where the terminal nodes are files and the non-terminals are directories, you might want only files to be selectable. You'd have to write code to block selection of non-terminals.

An example of a data-aware JdbTree: suppose that you travel widely and want to organize your travel photographs in a database. You plan to group the photos by place, but a list of town and city names won't work well because there may be duplicates. Instead, you create a tree of places you've traveled. At the top level of the tree you list continents. For each continent, you list countries on that continent. This is the second level of the tree. Towns and cities in each country make up the third level. You set the tree's dataSet and column properties to the Location column of the photographs data set. Now when you enter a new row in the data set, you can pick a location by selecting it in the tree, and you're guaranteed that only locations defined in the tree are used.

Loading a tree: Unfortunately, there's no natural, standard way to represent a hierarchy in a data set. As a result, JBuilder can't fill a JdbTree or JdbNavTree with data automatically. You have to build the tree through code. To do this

  1. Create a root node.
  2. Place a data item in a new node and make that node a child of the root.
  3. Continue creating nodes, making them children of any existing node.
  4. Associate the complete data structure, which is a Swing tree model, with a tree component.
Loading the tree tells how we built the tree for this help system.