Java自定义树模型更新问题

发布于 2024-12-06 00:49:31 字数 739 浏览 0 评论 0原文

这是树的结构:

根 - 分支 --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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄司 2024-12-13 00:49:31

如果没有看到代码,很难确定 - 不过我会打赌我的猜测:您不会通知 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)

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:

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

If it is of type MutableTreeNode, the easier way is via the convenience methods in DefaultTreeModel

 model.insertNodeInto(node, myBranch, position)
獨角戲 2024-12-13 00:49:31

看起来像是 Swing 中的并发 的问题,也许更新是在 EDT 之外,

您添加了新对象,然后测试 DefaultTreeModel 是否包含新对象,如果对象存在,则必须将(所有更新)包装到 invokeLater 中,对于对于 invokeAndWait 来说,SerializedObservate 会更好

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) into invokeLater, for Serializable or Observate would be better look for invokeAndWait

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文