Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

how to update jtree after removing or adding new node?

814246Feb 1 2013 — edited Feb 13 2013
after removing the nodes from jtree , nodes are still there but with no dotted lines.
i think my model is not updated properly or jtree is not getting notified properly.

Im using DefaultTableModel and my nodes are of user class which extends DefaultMutableTreeNode Class.

i tired method:

model.reload(node);
model.nodeStructureChanged(node);
model.nodeChanged(node);

but nothing is working properly. nodes are still there but only branch lines (dotted lines) are gone.

any suggestions!!

Comments

EJP
You only need to call model.reload(). It fires all the necessary events itself. See the Javadoc. But you must call it on the EDT thread.
814246
model.reload() will refresh complete tree while i need to refresh only selected node. since my tree is huge and it already takes long time to load. if i refresh whole tree for adding/removing single node it is going to take long time.
suggest me another solution.
EJP
I meant model.reload(node), the one you are calling. My suggestion is to omit the next two lines.
814246
model.reload(node) did not work. and i mean , i tired those methods one by one to see the change. but nothing happened.
i have succeeded in changing the already present node properties and reflect them in tree.

for that i used tree.setCellRenderer(new DefaultTreeCellRenderer());

first i set it to null and then later i assigned new CellRenderer. it worked properly. but for adding new node in parent is not happening.
In debugging its showing that new child is there in parent but jtree node reflecting that change.
walterln
You need to call reload/nodeStructureChanged on the parent node you've added/removed children to, not the child node.

Note this is not needed if you used DefaultTreeModel#insertNodeInto or #removeNodeFromParent which already fire the correct events.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 13 2013
Added on Feb 1 2013
5 comments
3,462 views