简体   繁体   English

在SWT.Tree中选择SWT.TreeItem

[英]Selecting an SWT.TreeItem within a SWT.Tree

So, I have the following code 所以,我有以下代码

tree.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        System.out.println("Clicked Item>>" + tree.getSelection()[0].toString());
    }
});`

How can I change it so that it fires only when I select an TreeItem within that Tree but on on expand or any other events? 如何更改它,使其仅在我在Tree选择TreeItem时才触发,而在展开或任何其他事件时才触发?

You could use JFace to handle it: 您可以使用JFace来处理它:

TreeViewer viewer = new TreeViewer(tree);
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
    @Override
    public void selectionChanged(SelectionChangedEvent event) {
        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        System.out.println("Clicked Item>>" + selection.getFirstElement());
    }
});

You will not be working with TreeItems though. 但是,您将不会使用TreeItems。 Pass your model to the viewer using setInput(). 使用setInput()将模型传递给查看器。 Set a content provider to handle your model and set a label provider to handle what is displayed. 设置内容提供者以处理模型,并设置标签提供者以处理显示的内容。

viewer.setLabelProvider(labelProvider);
viewer.setContentProvider(provider);
viewer.setInput(input);

Here is an example: http://www.vogella.com/tutorials/EclipseJFaceTree/article.html 这是一个示例: http : //www.vogella.com/tutorials/EclipseJFaceTree/article.html

Edit according to comment: 根据评论编辑:

Depending on the model you use, you might find out if the selected element has children from the model element. 根据您使用的模型,您可能会发现所选元素是否具有来自Model元素的子元素。 (eg a TreeNode object having a getChildren() or hasChilrden() method) (例如,具有getChildren()或hasChilrden()方法的TreeNode对象)
If you are using a ITreeContentProvider as content provider (which is probably true), you can do the following: 如果将ITreeContentProvider用作内容提供者(可能是这样),则可以执行以下操作:

IStructuredSelection selection = (IStructuredSelection) event.getSelection();
Object element = selection.getFirstElement();
System.out.println("Clicked Item>>" + element);
((ITreeContentProvider)viewer.getContentProvider()).hasChildren(element);

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

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