tags:

views:

90

answers:

4

Hi guys I use JTree with TreeNode which extending DefaultMutableTreeNode.when I add new node,i cant update JTree.Any help will be appreciated

+2  A: 

Adding to the node only is not enough, the Tree needs to be notified. The correct way to do this is to let the model take care of adding/removing nodes, as it will notify correctly the Tree on the way.

In case of a DefaultTreeModel (which should be the one used in the JTree if you haven't made your own TreeModel), you have the method insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index), which you can use.

Gnoupi
+3  A: 

Since Java use MVC to implement JTree component, your operation that add a node just change the model. Besides, you should notify representation of the tree some modifications have taken place. Try use fireTreeNodes*** methods of your tree model to notify the JTree object this kind of modification. Refer Java API for details. Good luck.

Summer_More_More_Tea
+1  A: 

Are you using DefaultTreeModel?

If so, when adding use insertNodeInto API so that appropriate events are generated.

ring bearer
Avoid duplicating answers, though, if you don't bring anything new.
Gnoupi
Buddy @Gnoupi - the down vote was uncalled for. We concurrently answered the question. I have seen in a few occasions that by the time I finish type my answer and post, a few new answers refresh.Suit yourself.
ring bearer
@ring - you answered it half an hour later, with saying basically the same thing, that's not what I would call "concurrently". Like I said already, avoid duplicating, as it ultimately brings clutter. If an answer is already stating what you have to say, you can simply not post yours, or delete if if already posted.
Gnoupi
A: 

if you use DefaultTreeModel try to call its reload() method.

DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.reload();

//or... model.reload(nodeUpdatedByYou);

Never use tree.updateUI() as this is a look and feel operation (although it works)

Serge Merzliakov