[英]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
. 这是我用于在TreeViewer
( viewer
)中创建Tree
( t
)的示例代码,其中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.