如何在SWT中使用鼠标滚轮滚动滚动复合

时间:2014-05-27 04:35:21

标签: java swt mousewheel

我想知道是否可以使用鼠标滚轮滚动ScrolledComposite。默认情况下它不起作用。

3 个答案:

答案 0 :(得分:2)

显然,有必要为复合材料创建鼠标滚轮侦听器。你可以使用这样的基础:

    scrolledComposite = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
    GridData scrollGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    scrolledComposite.setLayoutData(scrollGridData);
    layout = new GridLayout();
    scrolledComposite.setLayout(layout);

    compositeWrapper = new Composite(scrolledComposite);
    compositeWrapper.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    compositeWrapper.setLayout(layout);
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setExpandVertical(true);

    scrolledComposite.addListener(SWT.MouseWheel, new Listener() {
            public void handleEvent(Event event) {
                int wheelCount = event.count;
                wheelCount = (int) Math.ceil(wheelCount / 3.0f);
                while (wheelCount < 0) {
                    scrolledComposite.getVerticalBar().setIncrement(4);
                    wheelCount++;
                }

                while (wheelCount > 0) {
                    scrolledComposite.getVerticalBar().setIncrement(-4);
                    wheelCount--;
                }
            }
        });

答案 1 :(得分:2)

在谷歌搜索后,我找到了一个简单的解决方案,

scrolledComposite.addListener(SWT.Activate, new Listener() {
  public void handleEvent(Event e) {
    scrolledComposite.setFocus();
  }
});

答案 2 :(得分:0)

我不确定为什么@AlexanderGavrilov正在编写这么多代码,以下内容也适用于我:

scrolledComposite.addListener(SWT.MouseWheel, new Listener() {
    public void handleEvent(Event event) {
        scrolledComposite.getVerticalBar().setIncrement(e.count*3);
    }
});