您现在的位置是:首页 > 电脑 > 

如何获得相对于向导的控制边界(how to get control bounds relative to the wizard)

2025-07-26 17:06:13
如何获得相对于向导的控制边界(how to get control bounds relative to the wizard) 我希望我能解释一下我的问题。 我正在开发一个对话框,可以包含左侧的树木数量和右侧的一棵树。 我需要从左到右画一条线,但由于某种原因,我得到了相同的树项边界。 例如,当在树1中选择第一个项时,它将返回树2中第一个项的相同边界位置。 如何
如何获得相对于向导的控制边界(how to get control bounds relative to the wizard)

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

这是代码:

import draw2d.ColorCtants; import jface.viewers.ISelectionChangedListener; import jface.viewers.IStructuredSelection; import jface.viewers.SelectionChangedEvent; import jface.viewers.TreeViewer; import swt.SWT; import SashForm; import ScrolledComposite; import SelectionEvent; import SelectionListener; import swt.graphics.Rectangle; import swt.layout.FillLayout; import swt.layout.GridData; import swt.layout.GridLayout; import swt.widgets.Composite; import swt.widgets.Display; import swt.widgets.Event; import swt.widgets.Label; import swt.widgets.Listener; import swt.widgets.Shell; import 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.HORIZOTAL); Composite composite = new Composite(sashForm, SWT.OE); composite.setLayout(new GridLayout()); composite.setLayoutData(new GridData(GridData.FILL_BOTH)); composite.setBackground(ColorCtants.white); for (int i = 0; i < ; i++) { Label lbl = new Label(composite, SWT.OE); 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((SWT.DEFAULT, SWT.DEFAULT)); // Add content to scrolled composite Composite scrolledContent = new Composite(scrolledComposite, SWT.OE); 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(); println("Tree item bounds y " + bounds.y ); } } }); } new TreeViewer(sashForm); (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } }

I hope I can explain my problem. I am working on a dialog that can contain number of trees from left side, and one tree from right side. I need to draw a line from left to right, but for some reason, I am getting the same tree item bounds. For example, when the first item is selected in tree one it will return the same bounds location for the first item in tree two. How do I get item bounds relative to the wizard and not to the composite.

Here is the code:

import draw2d.ColorCtants; import jface.viewers.ISelectionChangedListener; import jface.viewers.IStructuredSelection; import jface.viewers.SelectionChangedEvent; import jface.viewers.TreeViewer; import swt.SWT; import SashForm; import ScrolledComposite; import SelectionEvent; import SelectionListener; import swt.graphics.Rectangle; import swt.layout.FillLayout; import swt.layout.GridData; import swt.layout.GridLayout; import swt.widgets.Composite; import swt.widgets.Display; import swt.widgets.Event; import swt.widgets.Label; import swt.widgets.Listener; import swt.widgets.Shell; import 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.HORIZOTAL); Composite composite = new Composite(sashForm, SWT.OE); composite.setLayout(new GridLayout()); composite.setLayoutData(new GridData(GridData.FILL_BOTH)); composite.setBackground(ColorCtants.white); for (int i = 0; i < ; i++) { Label lbl = new Label(composite, SWT.OE); 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((SWT.DEFAULT, SWT.DEFAULT)); // Add content to scrolled composite Composite scrolledContent = new Composite(scrolledComposite, SWT.OE); 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(); println("Tree item bounds y " + bounds.y ); } } }); } new TreeViewer(sashForm); (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } }

最满意答案

项边界始终相对于父树。

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

Point displayPos = tree.getTree().toDisplay(bounds.x, bounds.y);

然后使用以下命令将此位置转换为相对于另一个控件(例如shell)

Point pos = (displayPos);

The item bounds is always relative to the parent tree.

You can convert the position to be relative to the display using:

Point displayPos = tree.getTree().toDisplay(bounds.x, bounds.y);

and then convert this position to be relative to another control (such as your shell) using:

Point pos = (displayPos);

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/77673.html

相关标签:无
上传时间: 2023-04-27 11:42:55
留言与评论(共有 7 条评论)
本站网友 色散系数
0秒前 发表
I am getting the same tree item bounds. For example
本站网友 体重身高比例计算器
11分钟前 发表
SWT.HORIZOTAL); Composite composite = new Composite(sashForm
本站网友 生死线上
24分钟前 发表
如何获取相对于向导而不是复合的项目界限
本站网友 腾讯ceo马化腾
14分钟前 发表
0); treeItem2.setText("Level 2 Item "+ loopIndex2); } } } tree.getTree().setLayoutData(new GridData(SWT.FILL
本站网友 三无marblue
17分钟前 发表
SWT.FILL
本站网友 病退的条件
7分钟前 发表
SWT.HORIZOTAL); Composite composite = new Composite(sashForm