首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SWT表中的离散滚动

SWT表中的离散滚动
EN

Stack Overflow用户
提问于 2014-03-29 19:42:56
回答 1查看 454关注 0票数 0

如何在SWT Table (JFace TableViewer)中进行离散(逐行)滚动?

我需要一个Table来滚动“一次一个完整的行”,将一个完整的单元格放在顶部。

我使用JFace TableViewer,但没有找到向其添加鼠标侦听器的方法,因此我做了如下所示:

代码语言:javascript
运行
复制
TableViewer table = new TableViewer(shell, SWT.BORDER_DASH |SWT.FULL_SELECTION);
//some visual settings ommited here
table.getControl().addMouseWheelListener(new MouseWheelListener() {

        @Override
        public void mouseScrolled(MouseEvent e) {
            Table sourceControl = (Table)e.getSource();
            System.out.println(e.count);
            if(e.count >=0)
                sourceControl.setTopIndex(sourceControl.getTopIndex()-1);
            else
                sourceControl.setTopIndex(sourceControl.getTopIndex()+1);
        }
    });

但是事实证明,首先,如果e.count等于或更多,就会遗漏一些行。其次,有时setTopIndex()没有正确地放置行。

能否以更准确的方式进行呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-30 16:08:57

据我所知,在e.doit = false中添加一个Listener非常有效。下面是一个示例:

代码语言:javascript
运行
复制
public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout());

    final Table table = new Table(shell, SWT.NONE);

    for (int i = 0; i < 100; i++)
    {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText("Item " + i);
    }

    table.addListener(SWT.MouseWheel, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            e.doit = false;
            if (e.count >= 0)
                table.setTopIndex(table.getTopIndex() - 1);
            else
                table.setTopIndex(table.getTopIndex() + 1);
        }
    });

    shell.pack();
    shell.setSize(shell.getSize().x, 300);
    shell.open();

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

顶部的TableItem没有完全显示的唯一情况是,当您到达Table的末尾,而表的高度不是TableItem高度的确切倍数时。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22735967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档