1

In DefaultTreeModel, you can insert a node using the insertNodeInto() methods. However, I noticed that there also exists the nodesWereInserted() method that has this very terse Javadoc:

Invoke this method after you've inserted some TreeNodes into node.

I find it very strange that this is a public method. I would expect that if it were to fire off ChangeEvents to listeners that it ought to be a private method that is called by insertNodeInto(). It just seems that calling a method to notify of a change called by another method is somewhat inconsistent with how Java Swing is designed (I don't call an elementIsInserted() method when I add an element to a JComboBox).

But I discovered that in my code that it doesn't matter if I call nodesWereInserted() after insertNodeInto(). Either way the results turn out the same with the node being added to the tree and displaying in the GUI.

So could someone please explain if it is necessary to call nodesWereInserted() and similar methods in DefaultTreeModel? And if so, why? What does it do that insertNodeInto() can't do on its own?

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • As the Javadoc says that `insertNodeInto()` calls `nodesWereInserted(),` it's hard to see why you're surprised when it happens. – user207421 Dec 20 '13 at 06:16
  • @EJP, looks like I had a case of tunnel vision then. I was so focused on the Javadoc for that method that I didn't even consider looking at the other method I wanted to call. Although I'm still confused why `nodesWereInserted()` is a public method then if the only way to insert a node is to use `insertNodeInto()`. Subclassing maybe? – Thunderforge Dec 20 '13 at 06:20

3 Answers3

1

Imagine that your TreeModel uses an independent real tree and some nodes are inserted by separate process which is not aware of the model existing. Or two TreeModels use the same tree of objects (nodes). The process just adds children to some nodes. Or you insert node in the first model but also have to update the second.

Then you somehow should update model. In this case you use nodesWereInserted() and you need public method to be called from outside the model.

Of couse if you call insertNodeInto() the nodesWereInserted() is called inside

StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

You don't need to call it by yourself. As EJP said it's called every time when a node is inserted. You can override this function in your derived class, to get signaled when a node is inserted.

If you do not have your own derived classHowever you just add a TreeModellistener.

Joachim Weiß
  • 407
  • 2
  • 12
0

We can have our own custom tree node which is doing some processing. (Say we want to check if the new node is eligible for being inserted.) We add a new child to this custom tree node with the DefaultMutableTreeNode.insert() method. At this point, we need to inform the model about the insertion by calling the DefaultTreeModel.nodesWereInserted() method. If the modifications are too complex, it might be necessary to call the DefaultTreeModel.reload() method. If we do not have any custom processing in a our own tree node, then we can just add a new node by calling DefaultTreeModel.insertNodeInto().

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
  • it might be necessary to call the DefaultTreeModel.reload(), not to use proper models notifiers, but depends of structure, because is possible to create an ZOO – mKorbel May 31 '14 at 12:05