Search code examples
javaswingmodeljtreetreemodel

JTree and TreeModel with hierarchy of varying class?


I have a hierarchy of objects that I would like to show using a JTree and TreeModel. Each level of the hierarchy is a different class.

All the examples of JTree and TreeModel I've seen have a common class throughout the entire hierarchy of the tree (i.e. File for a file system browser). How do you go about implementing TreeModel when your hierarchy changes class from level-to-level? Do you put TreeModel inside TreeModel?


Solution

  • Implement TreeModel and then supply your own implementation of methods. JTree works with Objects, so you should be fine with multiple types.