Best way for modeling a JTree?

N

Nop.Lists

G'day,
I would be very grateful for insights considering modeling a JTree.

Background
=========
I am learning swing and try to build various kinds of trees.
One kind of tree I am dealing with is the filesystem tree (where my
underlying objects are java.io.File).
Another kind are XML files (where the underlying objects are e.g.
org.jdom.Entity).
Yet another kinds are trees of user defined objects.

The Question
==========
I have experienced with 3 methods for building the trees:
1. Use DefaultMutualTreeNode and put your object in teh UserNode
2. Define an overlying structure that extends the
DefaultMutualTreeNode and mirrors my domain.
Example: have a XMLEntity, XMLComment, XMLPI etc. wich correspond
to the entities of JDom.
3. Define a model over my domain (in the example of a filesystem tree
my model is based on java.io.File). I define all operations on the
model.


What would you consider as the best way and why?

Thank you very much for your insights!
nop.lists
 
P

Piet Blok

(e-mail address removed) wrote in @e23g2000prf.googlegroups.com:
G'day,
I would be very grateful for insights considering modeling a JTree.

Background
=========
I am learning swing and try to build various kinds of trees.
One kind of tree I am dealing with is the filesystem tree (where my
underlying objects are java.io.File).
Another kind are XML files (where the underlying objects are e.g.
org.jdom.Entity).
Yet another kinds are trees of user defined objects.

The Question
==========
I have experienced with 3 methods for building the trees:
1. Use DefaultMutualTreeNode and put your object in teh UserNode
2. Define an overlying structure that extends the
DefaultMutualTreeNode and mirrors my domain.
Example: have a XMLEntity, XMLComment, XMLPI etc. wich correspond
to the entities of JDom.
3. Define a model over my domain (in the example of a filesystem tree
my model is based on java.io.File). I define all operations on the
model.


What would you consider as the best way and why?

Thank you very much for your insights!
nop.lists

Hi,

1) I would definitely write a custom TreeModel and not
extend or use DefaultTreeModel.
2) I would consider the possibility to NOT define ALL
operations in that model, but, in certain cases, to
have the nodel listen to events fired from the domain
itself. (For File objects I can't think instantly of
an example of a possible event, but other domains may have).

As a sample, you might have a look at my abstract tree model

http://www.pbjar.org/docs/src/org/pbjar/swing/AbstractTreeModel.java

And an implementation for DOM trees

http://www.pbjar.org/docs/src/org/pbjar/dom/DOMTreeModel.java

Hope this helps

Piet
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top