在最后修改区域扩展Jtree? [英] expand Jtree at last modified area?

查看:110
本文介绍了在最后修改区域扩展Jtree?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dom4j从dom4j文档创建DocumentTreeModel。

I am using dom4j to create a DocumentTreeModel from a dom4j document.

我在 JScrollPane 中显示此DocumentTreeModel 。

I display this DocumentTreeModel inside JScrollPane.

我有一个按钮,为dom4j文档添加一个新节点,并重新创建DocumentTreeModel

I have a button that adds a new node to the dom4j document, and recreates the DocumentTreeModel

I我正在使用getPathForRow,但这看起来非常有限。我需要能够使用多个树深度。基本上寻找像 tree.getPathOfLastModifiedChildrensParent()

I am using getPathForRow but this seems pretty limited. I need to be able to work with multiple tree depth. Basically looking for something like tree.getPathOfLastModifiedChildrensParent()

onAddNewNodeButtonClickEventFired {
   dom4jdocument.addElement( "1" );                               
   tree.setModel(new DocumentTreeModel(dom4jdocument));                                
   tree.expandPath(tree.getPathForRow(1));                             
}  

基本上我试图获得 Jtree 每次编辑文档时重绘文档。

Basically I am trying to get the Jtree to redraw the document everytime document is edited.

推荐答案

看到你在编辑文档时设置新模型看起来仍然没有运行通知,对吧?如果是这样,你在JTree上不需要任何特殊的方法 - 你需要的是一个表现良好的TreeModel实现; - )

Seeing you setting a new model whenever you edit the document looks like you still don't have the notification running, right? If so, you don't need any special method on the JTree - what you need is a well-behaved implementation of TreeModel ;-)

为了好玩,我看了在DocumentTreeModel上:这是DefaultTreeModel上的一个非常小的封面,没有任何支持将Document中的更改粘贴到DocumentTreeModel中的更改。事实上Leaf- / BranchTreeNode仅实现TreeNode(而不是更进一步并实现MutableTreeNode)甚至禁用模型帮助器方法来插入/删除节点。简短的故事:所有的辛勤工作都留给你。

Just for fun, I looked up the DocumentTreeModel: that's an extremely small cover on top of DefaultTreeModel with no support whatever to glue changes in the Document to changes in the DocumentTreeModel. The fact that the Leaf-/BranchTreeNode implement TreeNode only (as opposed to going a step further and implement MutableTreeNode) even disables the models helper methods to insert/remove node. Short story: all the hard work is left to you.

基本上,您必须让treeModel知道底层Document中的任何更改。像(伪代码):

Basically, you have to make the treeModel aware of any change in the underlying Document. Something like (pseudo-code):

 DocNode newElement = document.addElement(...)
 DocNode parentElement = newElement.getParent();
 // walk the tree until you find the TreeNode which represents the DocNode
 BranchTreeNode root = treeModel.getRoot();
 BranchTreeNode parentNode = null;
 forEach (root.child)
     if child.getXMLNode().equals(parentElement)
          parentNode = child;
 // now find the childNode which corresponds to the new element
 forEach (parentNode.child)
    if (parentNode.child.getXMLNode().equals(newElement)
         childNode = child;
 // now notify the treeModel that an insertion has happened
 treeModel.nodesWhereInserted(parentNode, childNode ...)

嗯...在你的鞋子里我会寻找一个更舒适的实现,不能相信在某个地方还有其他实现吗?

Hmm ... in your shoes I would look for a more comfortable implementation, can't believe that there isn another implementation around somewhere?

CU
Jeanette

CU Jeanette

这篇关于在最后修改区域扩展Jtree?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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