【问题标题】:Programmatically scroll ExpandBar以编程方式滚动 ExpandBar
【发布时间】:2014-05-22 17:54:42
【问题描述】:

在我的代码中,org.eclipse.swt.widgets.ExpandBar 包含多个 ExpandItems。 ExpandBar 设置为滚动。如何以编程方式滚动ExpandBar?我寻找示例和 API,但没有运气。

【问题讨论】:

    标签: java swt scrollbar


    【解决方案1】:

    好的,将您的ExpandBar 包裹在ScrolledComposite 中,让它处理滚动。

    这样做的好处是ScrolledComposite 有一个名为.setOrigin(int, int) 的方法,您可以使用它来滚动到某个位置。

    下面是一些示例代码:

    public static void main(String[] args)
    {
        final Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setText("ExpandBar Example");
    
        final ScrolledComposite scrolledComp = new ScrolledComposite(shell, SWT.V_SCROLL);
    
        final ExpandBar bar = new ExpandBar(scrolledComp, SWT.NONE);
    
        for (int i = 0; i < 3; i++)
        {
            Composite composite = new Composite(bar, SWT.NONE);
            composite.setLayout(new GridLayout());
    
            for (int j = 0; j < 10; j++)
                new Label(composite, SWT.NONE).setText("Label " + i + " " + j);
    
            ExpandItem item = new ExpandItem(bar, SWT.NONE, 0);
            item.setText("Item " + i);
            item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
            item.setControl(composite);
        }
    
        bar.getItem(1).setExpanded(true);
        bar.setSpacing(8);
    
        /* Make sure to update the scrolled composite when we collapse/expand
         * items */
        Listener updateScrolledSize = new Listener()
        {
            @Override
            public void handleEvent(Event arg0)
            {
                display.asyncExec(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
                    }
                });
            }
        };
    
        bar.addListener(SWT.Expand, updateScrolledSize);
        bar.addListener(SWT.Collapse, updateScrolledSize);
    
        scrolledComp.setContent(bar);
        scrolledComp.setExpandHorizontal(true);
        scrolledComp.setExpandVertical(true);
        scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    
        shell.setSize(400, 200);
        shell.open();
    
        /* Jump to the end */
        scrolledComp.setOrigin(0, scrolledComp.getSize().y);
    
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    }
    

    运行后是这样的:

    如您所见,它已滚动到最后。


    更新

    好的,如果要跳转到特定项目的位置,请执行以下操作:

    添加Button 以测试功能。在Listener 中,获取 y 位置并滚动到它:

    Button jumpTo = new Button(shell, SWT.PUSH);
    jumpTo.setText("Jump to item");
    jumpTo.addListener(SWT.Selection, new Listener()
    {
        private int counter = 0;
        @Override
        public void handleEvent(Event e)
        {
            int y = getYPosition(bar, counter);
    
            /* Increment the counter */
            counter = (counter + 1) % bar.getItemCount();
    
            /* Scroll into view */
            scrolledComp.setOrigin(0, y);
        }
    });
    

    使用此方法获取y位置:

    private static int getYPosition(ExpandBar bar, int position)
    {
        /* Calculate the position */
        int y = 0;
        for(int i = 0; i < position; i++)
        {
            /* Incorporate the spacing */
            y += bar.getSpacing();
    
            /* Get the item (On LINUX, use this line) */
            ExpandItem item = bar.getItem(bar.getItemCount() - 1 - i);
            /* Get the item (On WINDOWS, use this line) */
            //ExpandItem item = bar.getItem(i);
    
            /* Add the header height */
            y += item.getHeaderHeight();
    
            /* If the item is expanded, add it's height as well */
            if(item.getExpanded())
                y += item.getHeight();
        }
    
        return y;
    }
    

    【讨论】:

    • 我试过了,但只有滚动条移动,客户区的内容/控件没有移动。然后我尝试重绘/更新。但它也不起作用。
    • @Tim 是的,你是对的。 ExpandBar 似乎没有收到事件。我会调查的。
    • 非常感谢您的及时答复。我会试试看。
    • Baz,我将为每个 ExpandItems 创建一个按钮,以通过您的示例以编程方式将其滚动到 ExpandBar 的顶部。有没有一种识别scrolledComp.setOrigin(x,y) 中y 值的好方法?当我得到 ExpandItem.getHeight() 时,ExpandItems 之间是否包含空格?
    • @Tim 更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2011-01-16
    • 2011-01-15
    • 2010-11-13
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多