前段时间做mxgraph流程项目,mxgraph是纯java编写的,流程图有鼠标滚动放大缩小的功能,但是要嵌入到swt中,问题就是嵌入后鼠标滚动事件消失,AWT组件是无法自行接收鼠标滚轮事件尝试各种方法均未找到合适的解决方法,这可能是用这种方式后的一个bug。先直接贴出我用的解决方法:
//AWT和SWT框架联系在一起的桥梁
final Composite container = new Composite(parent ,SWT.EMBEDDED);
Frame frame = SWT_AWT.new_Frame(container);
java.awt.event.MouseMotionListener mlAwt = new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
container.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
container.forceFocus();
}
});
}
@Override
public void mouseDragged(MouseEvent e) {
}
};
container.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseScrolled(final org.eclipse.swt.events.MouseEvent event) {
// BPMClient.moseWheelListener(event.count);
EventQueue.invokeLater(new Runnable() {
public void run() {
System.out.println("滚动事件传递滚动值,
这里根据event.count值判断是上下滚动");
}
});
}
});
// 给相应的面板传递该事件,提醒AWT鼠标监听器添加到所有被添加进了AWT框架的孩子
bpmPanel.graphComponent.getGraphControl().addMouseMotionListener(mlAwt);