expand Jtree at last modified area?
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. 开发者_运维百科
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.
精彩评论