KJW
KJW

Reputation: 15251

Refreshing JtreeModel does not work but Jtree updates accordingly

I am using org.dom4j.swing.DocumentTreeModel to construct a Swing Jtree from a parsed DOM document. When I edit the document which the treeModel is constructed from, I expect the Jtree to reflect this immediately by treeModel.reload();

The problem is that when the Jtree is fully collapsed first and then expanded by clicking the tree, it shows the 3 "test" elements. However, at this point, when I add more test elements, this change no longer is reflected! Why is it when Jtree swing is collapsed state, it is able to reflect the changes made by adding additional "test" elements to the tree but not when the Jtree swing is expanded.

I fully expected more "test" elements being appended realtime regardless of whether the swing JTree is open or not.

org.dom4j.swing.DocumentTreeModel treeModel = null;

DemoConstructor(){
         DOMReader dr = new DOMReader();
         org.dom4j.Document dom4jdocument = dr.read(browser.getDocument());

        //Create a split pane and display the tree .
        JScrollPane scrollPane = new JScrollPane(createTree(dom4jdocument));
ProgrammaticallyCalledToEditDoc();
ProgrammaticallyCalledToEditDoc();
ProgrammaticallyCalledToEditDoc();
}

private static void ProgrammaticallyCalledToEditDoc(){
dom4jdocument.addElement("test");
}

    private static Component createTree(org.dom4j.Document document) {
            treeModel = new DocumentTreeModel(document);
            TreeModelListener l = new TreeModelListener() {

                @Override
                public void treeStructureChanged(TreeModelEvent e) {
                    // TODO Auto-generated method stub
                    treeModel.reload();
                }

                @Override
                public void treeNodesRemoved(TreeModelEvent e) {
                    // TODO Auto-generated method stub
                    treeModel.reload();
                }

                @Override
                public void treeNodesInserted(TreeModelEvent e) {
                    // TODO Auto-generated method stub
                    treeModel.reload();
                }

                @Override
                public void treeNodesChanged(TreeModelEvent e) {
                    // TODO Auto-generated method stub
                    treeModel.reload();
                }
            };;;
            treeModel.addTreeModelListener(l);
            JTree tree = new JTree(treeModel);


            return tree;
        }

Upvotes: 0

Views: 749

Answers (1)

kleopatra
kleopatra

Reputation: 51524

most probably DocumentTreeModel doesn't fire the appropriate TreeModelEvents as is its task. There is no sense in doing a reload in the listener (that's complete circular would events be thrown ;-), the JTree itself is listening to the model and knows what to do on receiving a inserted. To check, insert simple println in the listener methods.

edit (in answer to the comment "what am I supposed... "- here it's easier)

1) nothing - at least nothing which effects the model: with a well-behaved model you'll end up in an endless loop (see below)

2) the loop (aka: complete circular) is:

model fires event --> listener receives event --> listener modifies model (model.reload) --> model fires event --> ...

Upvotes: 4

Related Questions