expand Jtree at last modified area?

老子叫甜甜 提交于 2019-12-06 02:10:10

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 ;-)

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.

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

Try - tree.revalidate(); It should refresh the component tree.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!