添加节点时未显示 Jtree!

发布于 2024-11-14 03:05:54 字数 1062 浏览 2 评论 0原文

我在滚动窗格中有一个静态 jtree。现在我正在尝试将子项添加到jtree,它已成功添加。现在,添加的节点在 jtree 中不可见。

尝试1:我尝试过 model.reload()

DefaultTreeModel model = (DefaultTreeModel) (tree.getModel());
TreePath Path = findByName(tree, new String[]{Globals.CONS_UIAPROJECTS});
DefaultMutableTreeNode pnode = (DefaultMutableTreeNode) Path.getLastPathComponent();
model.insertNodeInto(UIAProjectExploreDisplay.nodeProTree, pnode, pnode.getChildCount());
model.reload();

尝试2:我也尝试过tree.setModel()...

DefaultTreeModel model = (DefaultTreeModel) (tree.getModel());
TreePath Path = findByName(tree, new String[]{Globals.CONS_UIAPROJECTS});
DefaultMutableTreeNode pnode = (DefaultMutableTreeNode) Path.getLastPathComponent();
model.insertNodeInto(UIAProjectExploreDisplay.nodeProTree, pnode, pnode.getChildCount());

DefaultTreeModel projectTreeModel = new DefaultTreeModel((DefaultMutabletreeNode) model.getRoot());
tree.setModel(projectTreeModel);

即使如此,新添加的子项也不可见。但它被添加到树中。我通过打印孩子进行了测试。 给我建议一个解决方案。

I have a static jtree in a scrollpane. And now iam trying to add child to jtree, it got added successfully. Now, the added node is not visible in the jtree.

Try 1: I have tried with model.reload()

DefaultTreeModel model = (DefaultTreeModel) (tree.getModel());
TreePath Path = findByName(tree, new String[]{Globals.CONS_UIAPROJECTS});
DefaultMutableTreeNode pnode = (DefaultMutableTreeNode) Path.getLastPathComponent();
model.insertNodeInto(UIAProjectExploreDisplay.nodeProTree, pnode, pnode.getChildCount());
model.reload();

Try 2: I have tried with tree.setModel() too...

DefaultTreeModel model = (DefaultTreeModel) (tree.getModel());
TreePath Path = findByName(tree, new String[]{Globals.CONS_UIAPROJECTS});
DefaultMutableTreeNode pnode = (DefaultMutableTreeNode) Path.getLastPathComponent();
model.insertNodeInto(UIAProjectExploreDisplay.nodeProTree, pnode, pnode.getChildCount());

DefaultTreeModel projectTreeModel = new DefaultTreeModel((DefaultMutabletreeNode) model.getRoot());
tree.setModel(projectTreeModel);

Even then newly added child is not visible. but Its getting added in thr tree. I tested by printing the child.
Suggest me a solution.

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

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

发布评论

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

评论(2

压抑⊿情绪 2024-11-21 03:05:54

只需尝试以下行:

model.reload();

检查这个简单的示例。它对我有用,并在 5 秒延迟后显示两个新节点。

/**
 * @param args
 */
public static void main(String[] args) {
    TestTree frame= new TestTree();
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.pack();
    MutableTreeNode root = (MutableTreeNode)tree.getModel().getRoot();
    System.out.println("A: "+root.getChildCount());     
    try {
        Thread.sleep(5000);
    }
    catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    root.insert(new DefaultMutableTreeNode("test"), 1);
    ((DefaultMutableTreeNode)root.getChildAt(2)).insert(new DefaultMutableTreeNode("test2"), 1);
    ((DefaultTreeModel)(tree.getModel())).reload();
    System.out.println("B: "+root.getChildCount());
    System.out.println(root.getChildAt(1));
}

Simply try the following line:

model.reload();

Check this simple example. It works for me and shows both new nodes after the 5 seconds delay.

/**
 * @param args
 */
public static void main(String[] args) {
    TestTree frame= new TestTree();
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.pack();
    MutableTreeNode root = (MutableTreeNode)tree.getModel().getRoot();
    System.out.println("A: "+root.getChildCount());     
    try {
        Thread.sleep(5000);
    }
    catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    root.insert(new DefaultMutableTreeNode("test"), 1);
    ((DefaultMutableTreeNode)root.getChildAt(2)).insert(new DefaultMutableTreeNode("test2"), 1);
    ((DefaultTreeModel)(tree.getModel())).reload();
    System.out.println("B: "+root.getChildCount());
    System.out.println(root.getChildAt(1));
}
若言繁花未落 2024-11-21 03:05:54

我想也许你忘记在 TreeNode 的插入方法中设置新节点的父节点......这是我的代码

public void insert(MyTreeNode newChild, int childIndex) {
    if (!allowsChildren) {
        throw new IllegalStateException("node does not allow children");
    } else if (newChild == null) {
        throw new IllegalArgumentException("new child is null");
    }

        newChild.setParent(this);
        if (children == null) {
            children = new ArrayList<GoodSort>();
        }
        children.add(childIndex,(GoodSort)newChild);
        ((GoodSort)newChild).parent = this;
}

I think maybe you forget to set your new Node's parent in your TreeNode's insert method...here is my code

public void insert(MyTreeNode newChild, int childIndex) {
    if (!allowsChildren) {
        throw new IllegalStateException("node does not allow children");
    } else if (newChild == null) {
        throw new IllegalArgumentException("new child is null");
    }

        newChild.setParent(this);
        if (children == null) {
            children = new ArrayList<GoodSort>();
        }
        children.add(childIndex,(GoodSort)newChild);
        ((GoodSort)newChild).parent = this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文