Skip to main content

? DefaultTreeTableModel - how do I get the indices of the children ?

12 replies [Last post]
zaczek
Offline
Joined: 2006-07-04
Points: 0

What is the best way to take a TreeTable model and retrieve the indices of all of the children? Do I have to write some recursive method to go through the tree and test if children exist and whether nodes are collapsed?

I need to be able to go through a tree, figure out the current index of each branch/node and if branch is collapsed, then the nodes of this branch will share the same index.

BTW, I need to know if a branch is collapsed but I don't know if DefaultTreeTableModel gives me this info or if I have to get this info from TreeTable itself...anyone?

Message was edited by: zaczek

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11
Points: 0

actually, to me that doesn't appear a problem of the JXTreeTable but of your (mis-) understanding of how a treeModel and a JTree work. Just try the same thingy with a core JTree and the treeTableModel you'r using (after all, it's a TreeModel), make it work (with the help of some basic, core forum answers). If you have that working, try to do the same with a JXTreeTable. Then, if there are problems in the latter setup which haven't be there in the former - that's the point where this forum can help.

Cheers
Jeanette

zaczek
Offline
Joined: 2006-07-04
Points: 0

One other question...if their index is -1 (of the collapsed nodes), how can you access those nodes in the tree? Is there any way to get their reference? Is there anyway for me to traverse the tree?

Message was edited by: zaczek

kschaefe
Offline
Joined: 2006-06-08
Points: 0

What is the purpose of this tree traversal and node querying? Perhaps, if I am able to understand your intent I can offer better advice. What exactly do you want to do with the dumped values?

Karl

zaczek
Offline
Joined: 2006-07-04
Points: 0

I have a JSplitPane with a JXTreeTable on the left that contains a list of Timeline objects. On the right side is a JPanel where I draw the timelines (events drawn, etc).

The events (corresponding to a timeline) drawn on the right have to line up with the timelines on the left...when I collapse a branch on the left that contains multiple timelines, then the right side should do the same...ie hide the timelines.

SO, I need to know which of my timelines on the left are collapsed or at least what their current index is. I can't edit the "Timeline" to track an index inside it, so I figured the best way is to use the tree on the left to tell me the indices of timelines that are collapsed, etc.

What really sucks is that with the TreeTableModel I can figure out the ids of the nodes, which I could then use along with the JTreeTable to determine which of them is not collapsed....but the minute you collapse them then all of the indices get screwed up and I have no way of tying the two together: model and treetable . If there was some way that the treetable preserved the original/expanded ids of the nodes then I would be able to simply traverse the model and use the current index to figure out if the node is collapsed or not. But you can't do this because the minute you collapse a damn node and the subnodes disappear, the other nodes have their indices adjusted.

I cannot believe this is so difficult to do.

Message was edited by: zaczek

Message was edited by: zaczek

kschaefe
Offline
Joined: 2006-06-08
Points: 0

You probably want to have all the data in both sides and use a TreeExpansionListener on the tree table to listen for the expanding/collapsing of a node and then display or hide that appropriately.

Karl

zaczek
Offline
Joined: 2006-07-04
Points: 0

The problem is that I don't want to have the TreeTable be sent to the Panel on the right side because I won't always have a TreeTable. The right side will get the set of Timeline data and it will know the structure but the collapsed/expanded information was to be provided by the tree or some external source that could mimic a tree.

This is so damn frustrating...I can't believe that the TreeTable has a isCollapsed() method that takes a TreePath, and the TreeTableModel has a getPathToRoot() method but it does NOT return a TreePath. Instead it returns a TreeTableNode and that is not compatible...damn it to heck!

zaczek
Offline
Joined: 2006-07-04
Points: 0

Wait, I may have had a minor breakthrough...since the model has a getPathToRoot() method that returns a TreeTableNode[], I decided to use it to create a TreePath and then pass this TreePath to the isCollapsed of my JTreeTable and it might be working.

Here is sample code of the recursive method:

[code]
public currentRowIndex;

public void test() {
currentRowIndex = 0;
Traverse(myModel, myModel.getRoot());
}

public void Traverse( DefaultTreeTableModel model, Object o) {
int count = model.getChildCount(o);

for (int i=0; i currentRowIndex++;
TreeTableNode child = (TreeTableNode) model.getChild(o, i);
TreeTableNode[] path = model.getPathToRoot(child);

TreePath tpath = new TreePath(path);

if (model.isLeaf(child)) {
System.out.println("LEAF Index: " + currentRowIndex + " " + myTreeTable.isCollapsed(tpath));
} else {
System.out.println("BRCH Index: " + currentRowIndex + " " + myTreeTable.isCollapsed(tpath));
Traverse(model, child);
}
}
}
[/code]

Message was edited by: zaczek

zaczek
Offline
Joined: 2006-07-04
Points: 0

btw, kschaefe, thank you for the discussion and the help.

kschaefe
Offline
Joined: 2006-06-08
Points: 0

There is no way to take a TreeTableModel to perform those tasks. The visual state of a node (collapsed or not) is never known by the model (and never should be). You'll need to query the JXTreeTable directly, since it's the only thing that knows about the visual states that interest you.

Karl

zaczek
Offline
Joined: 2006-07-04
Points: 0

So, what is the best way for me to do the following:

Say I have a table

[code]
- Root index: 0 collapsed: False
- Node 1 1 F
- Node 2 2 F
- Branch 1 3 F
- Node 3 4 F
- Node 4 5 F
- Branch 2 6 F
- Node 5 7 F
- Node 6 8 F
[/code]

but now, I collapse "Branch 1" so I have the following:
[code]
- Root index: 0 collapsed: False
- Node 1 1 F
- Node 2 2 F
+ Branch 1 3,3,3 True,True,True
- Branch 2 4 F
- Node 5 5 F
- Node 6 6 F
[/code]
where Node 3, Node 4 have now an index of '3' to match their parent "Branch 1",
Is this true? Do their indices change?

also I assume Node 3, Node 4 are also marked as collapsed, like their parent.
Is this also true?

How do I go about getting any of this info? How can I get a list of indices or a list of collapse booleans for each of the rows of the tree?

Corrected questions...had "Nodes 4,5", but meant to ask about "Nodes 3,4"

Message was edited by: zaczek

kschaefe
Offline
Joined: 2006-06-08
Points: 0

I'm not sure I follow what you're trying to do, but...

Nodes 3 and 4 (you said 4 and 5, but I think you meant 3 and 4) are no longer visible when their parent is collapsed. There index according to the table is -1, signifying that the path is not part of the display.

The tree may also report that the leaves are collapsed (this is the default behavior for JTree).

Karl

zaczek
Offline
Joined: 2006-07-04
Points: 0

oops...yeah, I mean Nodes 3 and 4.

Ok, so I won't be able to get the indices of these collapsed/hidden nodes and that sucks.

But then there is no way for me to retrieve their collapse status either since the tree table isCollapsed expects a TreePath (I don't have this) or an integer of the row...but since the indices are hidden I would get -1 for that number and that won't work.

Sigh....this blows.

I'm gonna go try my hand at recursively traversing the tree and maybe I can get what I want.

thanks. and good catch on my node number discrepancy.

Message was edited by: zaczek