visualizing composite pattern

S

steffen.mazanek

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. The classes are as follows:


Category consists of Elements

Publication and Category are subclasses of Element

Is there a standard helper class I can use?


Thank you and bye,
Steffen
 
T

Thomas Weidenfeller

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top