JTree如何响应已更改的DefaultMutableTreeNode? [英] how does a JTree respond to a changed DefaultMutableTreeNode?

查看:172
本文介绍了JTree如何响应已更改的DefaultMutableTreeNode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要在非EDT线程中更改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?

我不是在谈论DefaultTreeModel事件,即insertNodeInto和removeNodeFromParent,我很清楚它们应该始终在EDT中运行... 我认为...

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

在更改节点用户对象的情况下,似乎JTree.TreeModelHandler是正在为此类事件监听"的东西……但是有理由期望监听器只会收到此类通知.在事件发生的线程中更改?并仅在同一个线程中传播其响应吗?

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.

那么,这是否意味着实际上必须在EDT中进行JTree节点中的大多数更改,否则可能会导致某些事情没有按您期望的那样发生?

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?

在DefaultMutableTreeNode的API中,确实确实说您必须自己进行同步" ...

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

推荐答案

Swing线程规则非常简单:Swing是单线程的,所有涉及Swing组件的操作都应在EDT上进行.

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

因此,一旦在JTree上设置了包含DefaultMutableTreeNodeTreeModel,您最好确保对模型(或节点的)所做的任何更改以及触发的相应事件都在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.

这篇关于JTree如何响应已更改的DefaultMutableTreeNode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆