mike rodent
mike rodent

Reputation: 15718

how does a JTree respond to a changed DefaultMutableTreeNode?

Just trying to work out what happens, in terms of threads, if you change the user object of a DefaultMutableTreeNode in a non-EDT thread?

I'm not talking about the DefaultTreeModel events, namely insertNodeInto and removeNodeFromParent, which I'm pretty clear should always be run in the EDT... I think...

In the case of changes to nodes' user objects, it appears that JTree.TreeModelHandler is the thing which is "listening" for such events... but is there reason to expect that the listener will only be notified of such a change in the thread where the event happened? And will propagate its response only in that self-same thread?

Obviously, therefore, I am assuming that this is a fairly basic "observer" pattern.

So does this mean that most changes in the nodes of a JTree must in fact be made to occur in the EDT, at the risk otherwise of things not happening as and when you expect?

In the API for DefaultMutableTreeNode it does indeed say that "you must do your own synchronizing"...

Upvotes: 1

Views: 542

Answers (1)

Robin
Robin

Reputation: 36621

The Swing threading rules are very simple: Swing is single-threaded, and all operations involving Swing components should happen on the EDT.

So as soon as your TreeModel containing DefaultMutableTreeNodes is set on a JTree you better make sure any changes you make to the model (or the node's) and the corresponding events which are fired happen on the EDT.

Upvotes: 4

Related Questions