如何在 JFace TreeViewer 中显示根元素?

发布于 2024-11-01 01:27:24 字数 67 浏览 1 评论 0原文

我有一个 JFace Treeviewer,但是它不显示作为输入传递的“根”元素。仅显示根的子级。 是否也可以显示根?

I have a JFace Treeviewer, however it does not display the 'root' element that is passed as input. Only the children of the root are shown.
Is it possible to display the root too?

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

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

发布评论

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

评论(3

泪之魂 2024-11-08 01:27:24

输入未确定为 TreeViewer 的根。事实上,树查看器不知道哪一个是根,哪一个不是。它由 contentprovider.getElements() 方法。我猜您很有可能在该方法内调用 getChildren() 。这会导致返回根元素的子元素,而不是返回根元素本身。

The input is not decided as the root of the TreeViewer. Infact the treeviewer doesn't know which one is root and which one is not. Its decided by the contentprovider.getElements() methods. With most probability, I guess you are calling the getChildren() inside that method. That leads to returning the children of the root elements rather than returning the root elements themselves.

蘑菇王子 2024-11-08 01:27:24

不应显示 TreeViewer 的输入元素。问题可以这样解决:

treeViewer.setInput("root");

在 ContentProvider 中:

public Object[] getElements(Object arg0) {
    return new Object[] { rootItem }; // your root item you want to display
}

The TreeViewer's input element should not be displayed. The problem can be solved like this:

treeViewer.setInput("root");

And in the ContentProvider:

public Object[] getElements(Object arg0) {
    return new Object[] { rootItem }; // your root item you want to display
}
美男兮 2024-11-08 01:27:24

我遇到了完全相同的问题,并通过使用布尔字段 treeInputSet 解决了它,如下所示,其中 Model 是要在树中显示的域类:

// Field to hold whether tree input was set
private boolean treeInputSet = false;    

// Other code...    

@Override 
public Object[] getElements(Object inputElement)
{
    if (inputElement instanceof Model)
    {
        if (treeInputSet)
        {
            Model model = (Model) inputElement;
            return model.getChildren().values().toArray();
        }
        else
        {
            treeInputSet = true;
            return new Object[] { inputElement };
        }
    }
    return null;
}

I ran into exactly the same problem and solved it by using a boolean field treeInputSet as follows in which Model is the domain class you want to display in the tree:

// Field to hold whether tree input was set
private boolean treeInputSet = false;    

// Other code...    

@Override 
public Object[] getElements(Object inputElement)
{
    if (inputElement instanceof Model)
    {
        if (treeInputSet)
        {
            Model model = (Model) inputElement;
            return model.getChildren().values().toArray();
        }
        else
        {
            treeInputSet = true;
            return new Object[] { inputElement };
        }
    }
    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文