Java自定义树模型更新问题
这是树的结构:
根 - 分支 --leafs
我用于 TreeModel DefaultTreeModel 和我的对象实现 TreeNode 接口
叶子是一些对象:
public class Leaf implements TreeNode
{
// implementation
分支有叶子列表:
public class Branch implements TreeNode
{
private List<Leaf> leafs;
// implementation
根是分支的容器:
public class Root implements TreeNode
{
private List<Branch> branches;
// implementation
当我添加新叶子时,我的树不会更新,当我添加叶子和使用已更新的根对象创建新的 DefaultTreeModel 。我观察 DefaultMutableTreeNode 实现,插入子项时没有触发任何事件...我做错了什么?之前,我尝试实现 TreeModel 接口,它看起来比为三个类实现 TreeNode 接口好得多,但结果相似。我也读过 GlazedLists,但我不喜欢他们的树概念。对我来说,最好的是实现 TreeModel 接口概念,但是当模型中的某些内部列表添加新元素时如何更新模型?...
Here is structure of the tree:
root
-branches
--leafs
I use for TreeModel DefaultTreeModel and my objects implement TreeNode interface
leaf is some Object:
public class Leaf implements TreeNode
{
// implementation
branch has List of leafs:
public class Branch implements TreeNode
{
private List<Leaf> leafs;
// implementation
And root is container of branches:
public class Root implements TreeNode
{
private List<Branch> branches;
// implementation
When I add new leaf, my tree doesn't updated, when I add leaf and create new DefaultTreeModel with my root object it's updated. I watch DefaultMutableTreeNode implementation, there isn't any event firing on inserting childs... What am I doing wrong? Before, I tried to implement TreeModel interface wich looks much better then implementing TreeNode interface for three classes, but result was similar. I also read about GlazedLists, but I dislike their tree conception. For me, the best is implementation TreeModel interface conception, but how to update model when some inner List in model add new element?...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有看到代码,很难确定 - 不过我会打赌我的猜测:您不会通知 TreeModel 有关您的插入的信息;-)
如果您的节点实现不是类型,则必须执行的操作的代码片段MutableTreeNode:
如果它是 MutableTreeNode 类型,更简单的方法是通过 DefaultTreeModel 中的便捷方法
Without seeing the code, it's hard to be sure - nevertheless I'll bet on my guess: you don't notify the TreeModel about your insertions ;-)
A code snippet of what you have to do if your node implementation is not of type MutableTreeNode:
If it is of type MutableTreeNode, the easier way is via the convenience methods in DefaultTreeModel
看起来像是 Swing 中的并发 的问题,也许更新是在 EDT 之外,
您添加了新对象,然后测试
DefaultTreeModel
是否包含新对象,如果对象存在,则必须将(所有更新)包装到invokeLater
中,对于对于invokeAndWait
来说,Serialized
或Observate
会更好that look like as issue with Concurrency in Swing, maybe updates are out of EDT,
you have add new Object, then to test
DefaultTreeModel
if contains new Object, if Objects exists, then you have to wrap (all updates) intoinvokeLater
, forSerializable
orObservate
would be better look forinvokeAndWait