how does a JTree respond to a changed DefaultMutableTreeNode?

南楼画角 提交于 2019-12-13 00:19:30

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/11072423/how-does-a-jtree-respond-to-a-changed-defaultmutabletreenode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!