简体   繁体   English

如何将根项目添加到SWT树

[英]How to add a root item to a SWT Tree

This is the sample code I'm using to create a Tree ( t ) in a TreeViewer ( viewer ), where MasterTreeContentProvider is a class that implements ITreeContentProvider . 这是我用于在TreeViewerviewer )中创建Treet )的示例代码,其中MasterTreeContentProvider是实现ITreeContentProvider的类。

Tree t = toolkit.createTree(client, SWT.NULL);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 20;
gd.widthHint = 100;
t.setLayoutData(gd);
toolkit.paintBordersFor(client);
section.setClient(client);
final SectionPart spart = new SectionPart(section);
managedForm.addPart(spart);
viewer = new TreeViewer(t);
viewer.addSelectionChangedListener(new ISelectionChangedListener() 
{
    public void selectionChanged(SelectionChangedEvent event) 
    {
        managedForm.fireSelectionChanged(spart, event.getSelection());
    }
});
viewer.setContentProvider(new MasterTreeContentProvider());
viewer.setInput(page.getEditor().getEditorInput()); 

Inside the MasterTreeContentProvider class I defined getElements() in such a way that it gets the contents (say Child1, Child2, Child3 ). MasterTreeContentProvider类内部,我定义了getElements()来获取内容(例如Child1, Child2, Child3 )。

With the present code it gets displayed in the following way: 使用当前代码,它将以以下方式显示:

Child1
Child2
Child3

and I would like to look the tree in this way, with the Root as below: 我想用这种方式看树,根如下:

Root
|-- Child1
 -- Child2
 -- Child3

How and where do I add a root element to this tree? 如何以及在哪里向该树添加根元素?

It is not enough to implement getElements() in your MasterTreeContentProvider . 在您的MasterTreeContentProvider实现getElements()MasterTreeContentProvider You have to implement getChildren() to build the hierarchy you want to show. 您必须实现getChildren()才能构建要显示的层次结构。 The Object you pass to those methods should be a custom class which represents this hierarchy. 传递给这些方法的Object应该是代表该层次结构的自定义类。 See Eclipse JFace Tree - Tutorial for a good example. 请参阅Eclipse JFace树-教程以获得一个很好的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM