Java 禁用SWT组合中的鼠标滚轮
我使用以下构造函数创建了一个组合:Java 禁用SWT组合中的鼠标滚轮,java,eclipse,swt,Java,Eclipse,Swt,我使用以下构造函数创建了一个组合: Composite scrolledComposite = new Composite(parent, SWT.V_SCROLL | SWT.H_SCROLL); 每次使用鼠标滚轮时,垂直滚动值都会更改 我知道这是默认行为,但我需要禁用它。我试图从组合中删除mouseweelllistener,但这似乎是一个本机调用。这是stacktrace,可以帮助理解我的问题 您可以在显示屏上添加一个过滤器,用于侦听SWT.mouseweel事件。下面是文本的一
Composite scrolledComposite =
new Composite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
每次使用鼠标滚轮时,垂直滚动值都会更改
我知道这是默认行为,但我需要禁用它。我试图从组合中删除mouseweelllistener
,但这似乎是一个本机调用。这是stacktrace,可以帮助理解我的问题
您可以在
显示屏上添加一个过滤器
,用于侦听SWT.mouseweel
事件。下面是文本
的一个示例,但它对复合
的作用相同:
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();
}
这将防止在第一个文本中滚动,但在第二个文本中也会起作用
请注意,在尝试滚动之前,您必须在文本内部单击,否则它将不会成为焦点控件。@DanielPeñalba很高兴我能提供帮助:)