问题
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.
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.

回答1:
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();
}
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.
来源:https://stackoverflow.com/questions/18613729/disable-mousewheel-in-a-swt-composite