expand Jtree at last modified area?

孤街浪徒 提交于 2019-12-07 12:05:32

问题


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

I display this DocumentTreeModel inside JScrollPane.

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

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

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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/5579580/expand-jtree-at-last-modified-area

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