3

这是树的结构:

根-分支--叶子

我用于 TreeModel DefaultTreeModel 并且我的对象实现了 TreeNode 接口

叶是一些对象:

public class Leaf implements TreeNode
{
   // implementation

分支有叶子列表:

public class Branch implements TreeNode
{
 private List<Leaf> leafs;

 // implementation

root 是分支的容器:

public class Root implements TreeNode
{
  private List<Branch> branches;

  // implementation

当我添加新叶子时,我的树不会更新,当我添加叶子并使用我的根对象创建新的 DefaultTreeModel 时,它会更新。我看 DefaultMutableTreeNode 实现,插入孩子时没有任何事件触发......我做错了什么?之前,我尝试实现 TreeModel 接口,它看起来比为三个类实现 TreeNode 接口要好得多,但结果相似。我也读过 GlazedLists,但我不喜欢他们的树概念。对我来说,最好的是实现 TreeModel 接口概念,但是当模型中的一些内部列表添加新元素时如何更新模型?...

4

2 回答 2

4

没有看到代码,很难确定——不过我敢打赌:你不会通知 TreeModel 你的插入;-)

如果您的节点实现不是 MutableTreeNode 类型,您必须执行的代码片段:

 // do the parent wiring in your custom TreeNode
 int position = myBranch.addChild(node);
 // notify the model 
 model.nodesWhereInserted(myBranch, new int[] {pos}); 

如果它是 MutableTreeNode 类型,更简单的方法是通过 DefaultTreeModel 中的便捷方法

 model.insertNodeInto(node, myBranch, position)
于 2011-09-23T08:01:54.013 回答
2

看起来像Swing 中的并发问题,也许更新超出了 EDT,

你已经添加了新的对象,然后测试DefaultTreeModel是否包含新的对象,如果对象存在,那么你必须将(所有更新)包装到invokeLaterSerializable或者Observate更好地寻找invokeAndWait

于 2011-09-23T08:07:33.300 回答