简体   繁体   中英

How can I reset a JTree?

I'm currently working with JTree, more precisely with CheckBoxTree, an inherited class created by JIDE. I need to find a way to reset the tree, meaning :

  • Clearing the selection
  • Erasing the nodes

I tried unsetting the Tree variable, the treeModel, and refreshing the UI, but it doesn't works.

Any ideas ?

Unsetting the variable alone won't help - all that will do is lose your copy of the reference to the JTree object.

What you need to do remove the reference the containing object holds to the JTree - I assume you have it some kind of GridContainer or Layout object - remove it from the parent ojbect and then call updateUI on that object.

For erasing the nodes you should get the tree model and clear it or set a new tree model.

To clear the selection, call clearSelection() on the tree (note that setModel(...) already calls clearSelection() so if you want to do both together, just set a new model and repaint).

Just rebuild the tree at the beginning of each iteration.

Something of this sort

RootNode=new CheckBoxTreeNode("root");
CheckBoxTree= new CheckBoxTree(RootNode);

would do the trick. No need to call updateUI. Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM