We help IT Professionals succeed at work.

JTree collapse when reload model

aquasw
aquasw asked
on
Medium Priority
495 Views
Last Modified: 2008-02-26
Hi,
I'm using a tree in my swing application. The tree model is changing all the time.
When I reload the tree model, the tree collapses and show it first hierarchy.
How can I set the tree to keep it collapses/expansion status after model reload?
Thanks.
Comment
Watch Question

hmm.

i would try adding a TreeModelListener to the TreeModel...

TreeModel model = new YourModel();
JTree tree = new JTree(model);

model.addTreeModelListener(
new TreeModelListener() {
public void treeStructureChanged(TreeModelEvent e) {
// and here, try make some record of the current expansion state
// and then recreate it... errr.... somehow.  sorry, im not sure
}

});
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you can't, as its displaying different data.
you'll need to save whats collapsed and whats not and expand the tree yourself as required.

Author

Commented:
objects thanks,
When I do it, I get flickering in the tree. It is not display good.
Any solution?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
don't reload your model :)
if the data is changing so much that u need to reload the model how can you retain the previously expaned nodes anyway (as they may no longer be there)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.