将ScrollableComposite位置设置为location [英] Set ScrollableComposite position to location
问题描述
使用SWT ScrollableComposite,有没有一种简单的方法可以将滚动条的位置设置为跳转,以使特定元素位于顶部?
Using the SWT ScrollableComposite, is there an easy way in which to set the scrollbar position to jump in such a way that a particular element will be positioned at the top?
例如,如果我有一个这样的复合材料,其中填充了26个标签,且标签的字母顺序依次为:
For example, if I had such a composite filled with 26 labels going down with the letters of the alphabet in order:
...然后,说我要将视图设置为"J"标签,并设置滚动条位置,如下所示:
...then, say that I want to set my view to the "J" label and have the scrollbar position set like this:
(这只是一个例子-如果我真的想做我在这里描述的事情,那么我显然会只使用一个列表框或一个表格来代替我的字母.)
这类似于Internet浏览器在跳转到页面内的特定标签时的工作方式.
This is similar to how Internet Browsers work when jumping to a specific tag within a page.
如有必要,可以通过大量手动测量计算来完成此操作,但我希望存在更简单的方法.
This can likely be done with a bunch of manual measurement calculations, if necessary, but my hope is that something simpler exists.
推荐答案
我相信您正在寻找ScrolledComposite
org.eclipse.swt.custom.ScrolledComposite.showControl(Control) //make it visible in view port
org.eclipse.swt.custom.ScrolledComposite.setOrigin(Point) //sets left corner coordinates, read SWT docs
最新答案:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Map<String,Control> controlMap = new HashMap<String,Control>();
final ScrolledComposite scrollComposite = new ScrolledComposite(shell,
SWT.V_SCROLL | SWT.BORDER);
final Composite parent = new Composite(scrollComposite, SWT.NONE);
for (int i = 0; i <= 50; i++) {
Label label = new Label(parent, SWT.NONE);
String index = String.valueOf(i);
controlMap.put(index, label);
label.setText(index);
}
GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parent);
scrollComposite.setContent(parent);
scrollComposite.setExpandVertical(true);
scrollComposite.setExpandHorizontal(true);
scrollComposite.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle r = scrollComposite.getClientArea();
scrollComposite.setMinSize(parent.computeSize(r.width,
SWT.DEFAULT));
}
});
shell.open();
Control showCntrl = controlMap.get(String.valueOf(5));
if(showCntrl != null){
scrollComposite.setOrigin(showCntrl.getLocation());
}
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
这篇关于将ScrollableComposite位置设置为location的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!