问题
I would like to know if it is possible to scroll a ScrolledComposite
using 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