java - 使用 ScrolledComposite 的 setOrigin 方法

标签 java scroll swt composite

如何使 ScrolledComposite 滚动以在顶部显示其中的指定控件?

这是我之前关于 how to make a ScrolledComposite programmatically scroll to a child control 的问题的后续内容。那里给出的建议给了我一些关于如何从概念上尝试它的想法,例如使用 setOrigin 方法,但我无法成功地使给定的示例和我自己的示例完全正常运行。这是我到目前为止构建的内容:

我制作了一个如下所示的外壳: enter image description here 我有一堆从 1 到 500 的标签,并且有一堆特定按钮,我想让 ScrolledComposite 以其各自的标签为中心,如下所示:

enter image description here

这是代码:

import java.util.HashMap;

public class ScrollableTest2 extends Shell {

    private static final int NUM_OF_LABELS = 500;
    private static final int[] SEEKABLES = new int[] {0, 9, 23, 99, 175, 176, 177, 178, 350, 495, 499};

    Map<Integer, Control> controlMap = new HashMap<Integer, Control>();

    private Composite cmpButtons;
    private Label vr;
    private ScrolledComposite scroller;
    private Composite cmpScrollInner;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String args[]) {
        try {
            Display display = Display.getDefault();
            ScrollableTest2 shell = new ScrollableTest2(display);
            shell.layout();
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the shell.
     * @param display
     */
    public ScrollableTest2(Display display) {
        super(display, SWT.SHELL_TRIM);
        createContents();
    }

    /**
     * Create contents of the shell.
     */
    protected void createContents() {
        setSize(400, 400);
        setText("SWT Application");
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 3;
        setLayout(gridLayout);

        cmpButtons = new Composite(this, SWT.NONE);
        cmpButtons.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
        cmpButtons.setLayout(new GridLayout(1, false));

        for (int seekable : SEEKABLES) {
            Button button = new Button(cmpButtons, SWT.NONE);
            button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
            button.setText("Go To " + String.valueOf(seekable+1));

            final int index = seekable;
            button.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    seekToLabel(index);
                }
            });
        }

        vr = new Label(this, SWT.SEPARATOR | SWT.VERTICAL);
        vr.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));

        scroller = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL);
        scroller.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        scroller.setExpandHorizontal(true);
        scroller.setExpandVertical(true);

        cmpScrollInner = new Composite(scroller, SWT.NONE);
        cmpScrollInner.setLayout(new GridLayout(1, false));

        scroller.setContent(cmpScrollInner);

        for (int i = 0; i < NUM_OF_LABELS; i++) {
            Label label = new Label(cmpScrollInner, SWT.NONE);
            label.setText("Label " + String.valueOf(i+1));
        }

        scroller.setMinSize(cmpScrollInner.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        scroller.setContent(cmpScrollInner);
        scroller.addControlListener(new ControlAdapter() {
            public void controlResized(ControlEvent e) {
                Rectangle r = scroller.getClientArea();
                scroller.setMinSize(cmpScrollInner.computeSize(r.width,
                        SWT.DEFAULT));
            }
        });

    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

    protected void seekToLabel(int index) {
        Control showCntrl = controlMap.get(new Integer(index));
        if(showCntrl != null){
            scroller.setOrigin(showCntrl.getLocation());
        }
    }

}

尽管我相信我正确设置了监听器,但这些按钮似乎根本没有执行任何操作。那么,我必须假设我滥用了 setOrigin 命令。我错过了什么?

最佳答案

哎呀别介意!事实证明,我只是忘记将控件添加到控件图中。一旦我这样做了,它就完美地工作了。

    for (int i = 0; i < NUM_OF_LABELS; i++) {
        Label label = new Label(cmpScrollInner, SWT.NONE);
        label.setText("Label " + String.valueOf(i+1));
        controlMap.put(new Integer(i), label);
    }

这毕竟是一个愚蠢的错误。

关于java - 使用 ScrolledComposite 的 setOrigin 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295967/

相关文章:

javascript - 在 Marionette Itemview 中渲染 reCaptcha V2.0 小部件

c# .NET 2.0 缩放滚动条

javascript - 如何使用 javascript 将滚动事件记录到控制台

java - 在 Java/SWT 中保存窗口状态

java - 如何在运行时增加文本框的高度?

java - 来自 fatjar 的带有 undertow 的静态资源

java - 如何检查给定对象是否是字符串中给定类名的实例?

Java 在新线程中打印的问题

Jquery ui 多选小部件在 IE 中缓慢滚动

java - 在 SWT 中将多个图像加载到滚动合成中