JTree: how keep expanded nodesexpanded on repaint

Hi experts!

I have an applet with a JTree. The user may expand/collaps its nodes. The applet is refreshed every x seconds because the tree displays data in a database, which i continuously updated. When repaint is run, all nodes collaps. I would like the nodes to keep their current state, collapsed or expanded.

The JTree exists in JPanels, wich exists in a JScrollPane. The repaint is done when the following code is run:

     scrollPanel.getViewport().repaint();

Any ideas on how do this?

Thanks for all help!!!
MarteJAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
example: my nodes are uniquely defined by their type + an ID (unique within the type)
In the
           Set myExpandedSet
I add all String values  = "<type as String>#<ID as String>".

I rebuild the tree.
Then, I traverse the tree and for each (new) node I construct that key "<type as String>#<ID as String>".
Is it in myExpandedSet => expand the node, if not do nothing.
I also remember what elements of myExpandedSet are used to expand.
The non-used ones I remove. They "talk" about nodes that are no longer available.
This way I avoid my myExpandedSet to grow forever.

Success MarteJ!
0
 
zzynxSoftware engineerCommented:
I do this by remembering what nodes are expanded. (in a Set with as value an object uniquely identifying the node)
After the tree is rebuilt with the up-to-date data, I apply the remembered information.
(i.e. traversing the tree and expand the nodes that I find in the remembered information)
0
 
zzynxSoftware engineerCommented:
BTW it's not the repaint that collapses all nodes.
It's because you "rebuild" your tree.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
objectsCommented:
You need to update data in the tree individually instead of relacing the entrie tree's data.
If you replace the entire tree with new data it has know way of knowing whether the existing selection expansion is valid so it cannot retain it.
0
 
zzynxSoftware engineerCommented:
>> If you replace the entire tree with new data it has no way of knowing whether the existing selection expansion
>> is valid so it cannot retain it.

That's indeed what I was trying to say in my last comment.
0
 
MarteJAuthor Commented:
Thanks for help!

>> You need to update data in the tree individually instead of relacing the entrie tree's data
Since the tree content (nodes) may vary from update to update, I cannot update each node individually. I need to replace the entire tree.

I will try to traverse the updated tree and apply the expanded state for each node that existed prior to the update.

0
 
objectsCommented:
> I need to replace the entire tree.

Then how can you maintain the expanded nodes if its a different tree :)
0
 
objectsCommented:
A node cannot keep its current state if it is no longer a part of the tree, and replace the entire tree will replace all nodes.
0
 
zzynxSoftware engineerCommented:
>> Then how can you maintain the expanded nodes if its a different tree :)
By the method I gave. I do it with success in my app.

It's a different tree, but (most of) the (new) nodes stand for the same database objects.
0
 
objectsCommented:
A lot simpler if you don't replace the entire tree, and instead update the nodes of the existing one :)
And what happens if a node changes location in the tree, would you still want it expanded in that case.
0
 
zzynxSoftware engineerCommented:
>> And what happens if a node changes location in the tree, would you still want it expanded in that case.
Yes, why not?

>> I will try to traverse the updated tree and apply the expanded state for each node that existed prior to the update.
If you're successful, don't you forget to close this Q by accepting one or more comments as answer? Thanks.
0
 
objectsCommented:
> Yes, why not?

It was just a question, there are different ways to handle that situation.

> I will try to traverse the updated tree and apply the expanded state for each node that existed prior to the update.

Ealier you said "I cannot update each node individually", no difference really :)
Instead of bothering with storing the previous state, you just traverse the tree and update each node according to new tree. That way existing node state is preserved.
0
 
MarteJAuthor Commented:
> Ealier you said "I cannot update each node individually", no difference really :)

The case is ths: data, with time tag, is put into the database by numerous components. The applet fetches data every 10 seconds for a user-specified time range, e.g. 1 hour. All data in the database that have time tags within the last hour will be displayed in the JTree. Every update (every 10 seconds) the collection of data is changed, the last 10 seconds of the 1 hour range is skipped, and data for the last 10 seconds are included, data with time tags within 1 hour in total. Therefore, a component might stop producing data and not be included in the fetch for the current applet update, and new components started producing data. The composition of the JTree is not known prior to an update.

>> I will try to traverse the updated tree and apply the expanded state for each node that existed prior to the update.

If I prior to an update collect the nodes that are expanded, I can traverse the new tree and expand the ones that exist in the collection.

> And what happens if a node changes location in the tree, would you still want it expanded in that case.

Yes, I want the node to be expanded, even if it has a new location in the tree.
0
 
mmuruganandamCommented:

TreePath[] path = tree.getSelectionPaths() ;

// update the tree

tree.setSelectionPaths(path);

Atleast, this will retain your selection path if that is not changed (i mean no new elements added in between the path).
0
 
zzynxSoftware engineerCommented:
Thanks for accepting, MarteJ.
That keeps us answering your future questions too.

You made my day :)
[ ...after all those left open questions... ]
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.