Java 如何获取相对于向导的控件边界

Java 如何获取相对于向导的控件边界,java,jface,composite,Java,Jface,Composite,我希望我能解释我的问题。 我正在制作一个对话框,它可以从左侧包含多棵树,从右侧包含一棵树。 我需要从左到右画一条线,但由于某些原因,我得到了相同的树项边界。 例如,当在树1中选择第一个项目时,它将返回树2中第一个项目的相同边界位置。 如何获取相对于向导而不是组合的项目边界 代码如下: import org.eclipse.draw2d.ColorConstants; import org.eclipse.jface.viewers.ISelectionChangedListener; impor

我希望我能解释我的问题。 我正在制作一个对话框,它可以从左侧包含多棵树,从右侧包含一棵树。 我需要从左到右画一条线,但由于某些原因,我得到了相同的树项边界。 例如,当在树1中选择第一个项目时,它将返回树2中第一个项目的相同边界位置。 如何获取相对于向导而不是组合的项目边界

代码如下:

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;

public class SWTSashForm
{
    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell(display);

        shell.setLayout(new FillLayout());

        SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);


        Composite composite = new Composite(sashForm, SWT.NONE);
        composite.setLayout(new GridLayout());
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));
        composite.setBackground(ColorConstants.white);


        for (int i = 0; i < 3; i++) {

            Label lbl = new Label(composite, SWT.NONE);
            lbl.setText("Tree " + i);

            // Configure scrolled composite
            ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
            scrolledComposite.setLayout(new GridLayout());
            scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            scrolledComposite.setExpandVertical(true);
            scrolledComposite.setExpandHorizontal(true);
            scrolledComposite.setAlwaysShowScrollBars(true);
            scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

            // Add content to scrolled composite
            Composite scrolledContent = new Composite(scrolledComposite, SWT.NONE);
            scrolledContent.setLayout(new GridLayout());
            GridData gridData = new GridData();
            gridData.horizontalAlignment = SWT.FILL;
            gridData.grabExcessHorizontalSpace = true;
            gridData.verticalAlignment = SWT.FILL;
            gridData.grabExcessVerticalSpace = true;
            scrolledContent.setLayoutData(gridData);
            scrolledComposite.setContent(scrolledContent);

            final TreeViewer tree = new TreeViewer(scrolledContent);
            for(int loopIndex0 = 0; loopIndex0 < 10; loopIndex0++) {
                TreeItem treeItem0 = new TreeItem(tree.getTree(), 0);
                treeItem0.setText("Level 0 Item "+ loopIndex0);

                for(int loopIndex1 = 0; loopIndex1 < 10; loopIndex1++) {
                    TreeItem treeItem1 = new TreeItem(treeItem0, 0);
                    treeItem1.setText("Level 1 Item "+ loopIndex1);

                    for(int loopIndex2 = 0; loopIndex2 < 10; loopIndex2++) {
                        TreeItem treeItem2 = new TreeItem(treeItem1, 0);
                        treeItem2.setText("Level 2 Item "+ loopIndex2);
                    }
                }
            }
            tree.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));


            tree.getTree().addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event e) {
                    TreeItem[] selection = tree.getTree().getSelection();
                    for (int i = 0; i < selection.length; i++){
                        TreeItem item = selection[i];
                        Rectangle bounds = item.getBounds();
                        System.out.println("Tree item bounds y " + bounds.y  );
                    }
                } });
        }
        new TreeViewer(sashForm);
        shell.open();

        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }

}
import org.eclipse.draw2d.ColorConstants;
导入org.eclipse.jface.viewers.iSelectChangedListener;
导入org.eclipse.jface.viewers.IStructuredSelection;
导入org.eclipse.jface.viewers.SelectionChangedEvent;
导入org.eclipse.jface.viewers.TreeViewer;
导入org.eclipse.swt.swt;
导入org.eclipse.swt.custom.SashForm;
导入org.eclipse.swt.custom.ScrolledComposite;
导入org.eclipse.swt.events.SelectionEvent;
导入org.eclipse.swt.events.SelectionListener;
导入org.eclipse.swt.graphics.Rectangle;
导入org.eclipse.swt.layout.FillLayout;
导入org.eclipse.swt.layout.GridData;
导入org.eclipse.swt.layout.GridLayout;
导入org.eclipse.swt.widgets.Composite;
导入org.eclipse.swt.widgets.Display;
导入org.eclipse.swt.widgets.Event;
导入org.eclipse.swt.widgets.Label;
导入org.eclipse.swt.widgets.Listener;
导入org.eclipse.swt.widgets.Shell;
导入org.eclipse.swt.widgets.TreeItem;
公共类SWTSashForm
{
公共静态void main(字符串[]args){
显示=新显示();
外壳=新外壳(显示);
setLayout(新的FillLayout());
窗框形式窗框形式=新窗框形式(壳体,SWT.水平);
复合材料=新复合材料(sashForm,SWT.NONE);
setLayout(新的GridLayout());
setLayoutData(新的GridData(GridData.FILL_BOTH));
复合材料。背景(颜色常数。白色);
对于(int i=0;i<3;i++){
标签lbl=新标签(复合,SWT.NONE);
lbl.setText(“树”+i);
//配置滚动组合
ScrolledComposite ScrolledComposite=新的ScrolledComposite(复合,SWT.V|U SCROLL | SWT.H|U SCROLL | SWT.BORDER);
scrolledComposite.setLayout(新的GridLayout());
setLayoutData(新的GridData(SWT.FILL,SWT.FILL,true,true));
scrolledComposite.setExpandVertical(true);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setAlwaysShowScrollBars(true);
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT,SWT.DEFAULT));
//将内容添加到滚动组合中
复合scrolledContent=新复合(scrolledComposite,SWT.NONE);
setLayout(新的GridLayout());
GridData GridData=新的GridData();
gridData.horizontalAlignment=SWT.FILL;
gridData.grabExcessHorizontalSpace=true;
gridData.verticalAlignment=SWT.FILL;
gridData.grabExcessVerticalSpace=true;
scrolledContent.setLayoutData(gridData);
scrolledComposite.setContent(scrolledContent);
最终TreeViewer树=新的TreeViewer(滚动内容);
对于(int-loopIndex0=0;loopIndex0<10;loopIndex0++){
TreeItem treeItem0=新的TreeItem(tree.getTree(),0);
treeItem0.setText(“0级项目”+loopIndex0);
对于(int-loopIndex1=0;loopIndex1<10;loopIndex1++){
TreeItem treeItem1=新的TreeItem(treeItem0,0);
treeItem1.setText(“1级项目”+loopIndex1);
对于(int-loopIndex2=0;loopIndex2<10;loopIndex2++){
TreeItem treeItem2=新的TreeItem(treeItem1,0);
treeItem2.setText(“二级项目”+loopIndex2);
}
}
}
setLayoutData(新的GridData(SWT.FILL,SWT.FILL,true,true));
tree.getTree().addListener(SWT.Selection,new Listener()){
公共无效handleEvent(事件e){
TreeItem[]selection=tree.getTree().getSelection();
for(int i=0;i
项目边界始终相对于父树

可以使用以下方法将位置转换为相对于显示器的位置:

Point displayPos = tree.getTree().toDisplay(bounds.x, bounds.y);
然后使用以下命令将此位置转换为相对于另一个控件(如shell):

Point pos = shell.toControl(displayPos);