How to scroll a scrolled composite using mouse wheels in SWT

孤者浪人 提交于 2019-12-11 02:50:38

问题


I would like to know if it is possible to scroll a ScrolledCompositeusing mouse wheels. By default it is not working.


回答1:


Apparently, it is necessary to create mouse wheel listener for your composite. You can use something like this as the basis:

    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--;
                }
            }
        });



回答2:


After googling around, I found a simple solution,

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



回答3:


I'm not sure why @AlexanderGavrilov is writing so much code, the following works for me as well:

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


来源:https://stackoverflow.com/questions/23881144/how-to-scroll-a-scrolled-composite-using-mouse-wheels-in-swt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!