javaswingdefaulttreemodel

Is it necessary to call nodesWereInserted() and similar methods in DefaultTreeModel?


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?


Solution

  • 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.