Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

How can I make JTree look at children over siblings?

Thread view: 
Oliver - 19 Jan 2006 05:12 GMT
I have a question which doesen't appear to be answered by the Sun JTree
tutorial.

The Sun JTree tutorial says that a TreeModel can allow me to use an
existing hierarchial structure. Ok, so I have a hierarchial structure
which is 'child based' not 'sibling based', and the default JTree view
doesen't cut it.

I'll give you an example which illustrates my problem. Assume a list of
chess moves; If you merely add the moves to the root node, this would
be acceptable as long as you did not need to show more than one
variation on a move. Assume at some point it is black's move in an
existing game. Adding a white move to the currently selected black move
will create a child of that black move - not a sibling. It even looks
like it's working. But if you try to add a second variation to that
move, it merely shows up as another move under the black move, not as a
repeat of the visual effect of adding the first variation.

The logical conclusion is that every move should be a child of the
previous move, not a sibling of it. This would allow me to "show next
move" by simply showing the variation symbols (a, b, c etc or 1, 2, 3)
directly on the board and letting the user click on them. Adding the
moves to the root node was the original mistake. Having the main line
of play stored as child #1, and variations as child #2, child #3 etc.
is the pre-existing hierarchial data structure I am faced with.

So the problem is how to make JTree dislpay child #1 as the so-called
"main line". I don't want to have to keep scrolling to the right, it
should display straight down for all first children. Should I even be
using JTrees for this? I think I should be able too, but I'm not sure
where I need to code away from the default behavior. TreeModel?
TreeCellRenderer? TreeModelListener? TreeChanger? TreePhaser? It's all
very confusing.

-Oliver
Thomas Weidenfeller - 19 Jan 2006 09:17 GMT
> The Sun JTree tutorial says that a TreeModel can allow me to use an
> existing hierarchial structure. Ok, so I have a hierarchial structure
> which is 'child based' not 'sibling based', and the default JTree view
> doesen't cut it.

There is only one way JTree displays tree data. I think the
classification for that type of graph is a planar, orthogonal,
upward-leftward level-drawing tree, but I would have to look it up.

If that layout doesn't cut it, forget JTree.

> I'll give you an example which illustrates my problem.

Sorry, no, it doesn't illustrate your problem at all. Children in a tree
are children, siblings in a tree are siblings. You want to have siblings
displayed as children, or children displayed as siblings? Or what? If
you really want that, transpose the tree.

> Assume a list of
> chess moves;

Why? Can't you express your problem in general terms? Not for me, for
you. Because from all your many words it is not clear if your problem is
with the visualization/display of your data, or with your data structure
itself. If you want a different graph layout or if you want to transpose
a tree, or what.

Do you have a clear understanding what a parent-child relation in your
internal data structure represents in your problem domain, and what a
sibling-sibling relation represents in your problem domain? Do you even
have a tree (or a forest), or do you have a different data structure at all?

> If you merely add the moves to the root node, this would
> be acceptable as long as you did not need to show more than one
> variation on a move. Assume at some point it is black's move in an
> existing game. Adding a white move to the currently selected black move
> will create a child of that black move - not a sibling.

But that's in your code, isn't it? If you want that some data is
recorded as a sibling of some other data, then make it a sibling in your
data structure, not a child. Why do yo complain that you inserted
something as a child in your data structure, and surprise, surprise, it
is a child in your data structure?

Once again, have you defined what a parent-child and sibling-sibling
relation means in your problem domain? Is that meaning fixed? If not,
JTree can for sure not read your mind and figure out when which relation
means what.

> It even looks
> like it's working. But if you try to add a second variation to that
> move, it merely shows up as another move under the black move, not as a
> repeat of the visual effect of adding the first variation.

"a repeat of the visual effect of adding the first variation."

Er?

> The logical conclusion is that every move should be a child of the
> previous move, not a sibling of it.

If you think so, then store it as a child.

> This would allow me to "show next
> move" by simply showing the variation symbols (a, b, c etc or 1, 2, 3)
> directly on the board and letting the user click on them.

Board? We are talking about a tree layout here. Why do you mix some
board representation into this? What is it what you want to get fixed?
Your board representation, your JTree representation, your internal data
structure?

> It's all very confusing.

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Oliver - 20 Jan 2006 01:22 GMT
> Thomas Weidenfeller wrote:
> > Oliver wrote:
[quoted text clipped - 6 lines]
> itself. If you want a different graph layout or if you want to transpose
> a tree, or what.

Your core misunderstanding seemed to be that I was complaining about
how JTree was visualising my data structure. I was not. I was merely
asking how to change how JTree visualised that data structure. When I
realised you were being adversarial because you thought I was
complaining I realised that I had to re-express only the question and
not the problem.

The premise of my question is found in the Sun Java Tutorial "How to
use Trees" found at
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html.
It states "...if you have a pre-existing hierarchical data structure,
you don't need to duplicate it or force it into the TreeNode mold. You
just need to implement your tree model so that it uses the information
in the existing data structure." (from section "Creating a Data Model")

So logically the question I asked stands. How do I code around a
treeModel to make it display children as siblings, and siblings as
children? And, is that even the right way to accomplish what I want to
do (which, if you read my previous message, is display first child as
the main line of play). It would seem transposing (you probably really
meant to say transforming) a tree is a wasted effort and not the Java
Way, which *may* (and hence my original post) be treeModel.

Oliver
Oliver Wong - 20 Jan 2006 18:17 GMT
> How do I code around a
> treeModel to make it display children as siblings, and siblings as
> children?

   Maybe I'm misunderstanding (my eyes did glaze over in your initial
chess-example post), but I don't think it is "possible" (perhaps
"reasonable" is the more correct word?) to do this transformation.

   The "sibling" relationship is symmetric: If A is a sibling of B, then B
is a sibling of A. The "child" relationship is anti-symmetric: If A is a
child of B, then B is guaranteed not to be a child of A (or else you don't
have a tree, you have a graph with cycles).

   So you can't just switch the "sibling" and "child" relationships, and
expect to end up with a tree.

   - Oliver (Wong)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.