【问题标题】:Disable MouseWheel in a SWT composite在 SWT 复合中禁用 MouseWheel
【发布时间】:2013-09-07 22:53:46
【问题描述】:

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

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

每次我使用鼠标滚轮时,垂直滚动值都会改变。

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

【问题讨论】:

    标签: java eclipse swt


    【解决方案1】:

    您可以将Filter 添加到侦听SWT.MouseWheel 事件的Display。这是Text 的示例,但它对于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 中滚动,但它会在第二个中起作用。


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

    【讨论】:

    • @DanielPeñalba 很高兴我能帮上忙 :)
    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 2011-01-31
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2012-11-16
    相关资源
    最近更新 更多