Hi everybody,
for a project i need to represent a tree in a comfortable
manner. There should be some kind of + a click on which
expands the tree successivley.
JTree is the widget for displaying such trees.
The classes are as follows:
Category consists of Elements
Publication and Category are subclasses of Element
The class hierarchy is largely irrelevant for a JTree. JTree works on a
tree model. That model is supposed to contain tree nodes. These nodes
contain references to children and the parent.
In order to display data with a JTree you are supposed to implement/use:
* TreeModel, if you want to implement your own model (don't forget the
firing of change events, then). Or DefaultTreeModel if you want to use a
default implementation of TreeModel. DefaultTreeModel already contains
the necessary event firing mechanisms. It is maybe a good idea to start
with it, instead of implementing an own TreeModel.
* TreeNode or MutableTreeNode, if you want to implement your own nodes.
Or DefaultMutableTreeNode, if you want to use a default implementation.
If you have existing classes, implementing MutableTreeNode might be the
better choice (esp. if you use DefaultTreeModel, which is more happy
with MutableTreeNode than with TreeNode).
Also:
* Always, really always, do changes to the tree via the
(Default)TreeModel. That way the necessary events are fired to the
widget. If you use an own TreeModel, take extra care to implement this
event firing.
* Do not use the JTree api to force repaints/redraws. If you think you
have to, you have messed up the TreeModel event firing. Fix that instead.
Finally:
* There is a separate GUI newsgroup: comp.lang.java.gui
* Check out Sun's Swing tutorial. It contains an own chapter about using
JTree
/Thomas