Solved

JTree: how keep expanded nodesexpanded on repaint

Posted on 2004-04-14
15
1,194 Views
Last Modified: 2007-12-19
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!!!
0
Comment
Question by:MarteJ
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10823824
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10823836
BTW it's not the repaint that collapses all nodes.
It's because you "rebuild" your tree.
0
 
LVL 92

Expert Comment

by:objects
ID: 10828016
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10830642
>> 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
 

Author Comment

by:MarteJ
ID: 10830895
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
 
LVL 92

Expert Comment

by:objects
ID: 10830956
> I need to replace the entire tree.

Then how can you maintain the expanded nodes if its a different tree :)
0
 
LVL 92

Expert Comment

by:objects
ID: 10830960
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 37

Expert Comment

by:zzynx
ID: 10830996
>> 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
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 10831033
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
 
LVL 92

Expert Comment

by:objects
ID: 10831062
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10831127
>> 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
 
LVL 92

Expert Comment

by:objects
ID: 10831164
> 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
 

Author Comment

by:MarteJ
ID: 10831683
> 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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10832270

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
 
LVL 37

Expert Comment

by:zzynx
ID: 10833353
Thanks for accepting, MarteJ.
That keeps us answering your future questions too.

You made my day :)
[ ...after all those left open questions... ]
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now