Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JTree: how keep expanded nodesexpanded on repaint

Posted on 2004-04-14
15
Medium Priority
?
1,264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

610 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