刷新JtreeModel不起作用但Jtree相应更新

发布于 2024-10-30 20:13:46 字数 2043 浏览 1 评论 0原文

我正在使用 org.dom4j.swing.DocumentTreeModel 从解析的 DOM 文档构造 Swing Jtree。当我编辑构建treeModel的文档时,我希望Jtree能够通过treeModel.reload()立即反映这一点;

问题是,当 Jtree 首先完全折叠,然后通过单击树展开时,它会显示 3 个“测试”元素。然而,此时,当我添加更多测试元素时,这种变化不再体现出来!为什么当 Jtree swing 处于折叠状态时,它能够反映通过向树添加额外的“测试”元素所做的更改,但当 Jtree swing 展开时却不能反映。

我完全期望实时附加更多“测试”元素,无论 swing JTree 是否打开。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-11-06 20:13:46

最有可能的是,DocumentTreeModel 不会像它的任务那样触发适当的 TreeModelEvents。在侦听器中进行重新加载是没有意义的(如果抛出事件,这就是完整的循环;-),JTree 本身正在侦听模型并知道在接收插入后要做什么。要进行检查,请在侦听器方法中插入简单的 println 。

编辑(回答评论“我应该做什么......” - 这里更容易)

1)什么都没有 - 至少没有什么会影响模型:使用一个行为良好的模型,你最终会陷入无限循环(参见下面)

2)循环(又名:完整循环)是:

模型触发事件 -->监听器接收事件 -->监听器修改模型(model.reload)-->模型触发事件 --> ...

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 --> ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文