本文介绍了在SWT合成中禁用MouseWheel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下构造函数创建了一个复合材料:

I created a composite using the following constructor:

Composite scrolledComposite =
    new Composite(parent, SWT.V_SCROLL | SWT.H_SCROLL);

每次使用鼠标滚轮时,垂直滚动值都会更改.

Each time I use the mouse wheel, the vertical scroll value is changed.

我知道这是默认行为,但是我需要禁用它.我试图从组合中 removeMouseWheelListener ,但是似乎这是一个本地调用.这是可以帮助理解我的问题的堆栈跟踪.

I know that is the default behavior, but I need to disable it. I tried to removeMouseWheelListener from the composite, but it seems that this is a native call. This is the stacktrace that could help to understand my problem.

推荐答案

您可以在 Display 上添加 Filter ,以监听 SWT.MouseWheel 事件.这是 Text 的示例,但对于 Composite 来说,其用法相同:

You can add a Filter to the Display that listens for SWT.MouseWheel events. Here is an example for Text, but it works identically for Composite:

public static void main(String[] args)
{
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    // This text is not scrollable
    final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));

    text.setText("a\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\n");

    // This is the filter that prevents it
    display.addFilter(SWT.MouseWheel, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            // Check if it's the correct widget
            if(e.widget.equals(text))
                e.doit = false;
            else
                System.out.println(e.widget);
        }
    });

    // This text is scrollable
    final Text otherText = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    otherText.setLayoutData(new GridData(GridData.FILL_BOTH));

    otherText.setText("a\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\n");

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

这将防止在第一个 Text 中滚动,但在第二个 Text 中将起作用.

This will prevent scrolling in the first Text, but it will work in the second one.

请注意,在尝试滚动之前,必须先在文本内部单击,否则它将不是焦点控件.

Note that you have to click inside the text before trying to scroll, because otherwise it would not be the focus control.

这篇关于在SWT合成中禁用MouseWheel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 22:52