【问题标题】:Discrete scrolling in SWT TableSWT 表中的离散滚动
【发布时间】:2017-04-23 18:41:15
【问题描述】:

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

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

我使用 JFace TableViewer,但我没有找到添加鼠标监听器的方法,所以我做了这样的事情:

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 等于或大于 3,则会丢失一些行。其次,有时setTopIndex() 没有正确放置行。

可以更准确地完成吗?

【问题讨论】:

    标签: java swt jface tableviewer


    【解决方案1】:

    据我所知,在Listener 中添加e.doit = false 效果很好。这是一个例子:

    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 高度的精确倍数时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多