本文介绍了MouseWheel事件不会在SWT-AWT组件中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了无法启动的鼠标滚轮事件.我使用swt_awt桥,因此可以在我的RCP应用程序中使用swing组件.我测试了我能找到的所有东西,但是没有成功.因为我的应用程序非常复杂,所以我创建了问题的简化版本,如果您想帮助我,还应该可以帮助您确定方向.

I am stuck at mousewheel event that just doesn't fire. I use swt_awt bridge, so I am able to use swing component in my RCP application. I tested everything I was able to find, but with no success. Because my application is awfully complex, I created a simplified version of my problem, which should also help you to orientate if you would like to help me.

现在,这个简化的问题是,我希望我的文本区域(或滚动窗格)能够捕获鼠标滚轮事件.但是这些事件丢失了.

Now this simplified problem is, that I want my textarea (or scrollpane) to catch mousewheel events. But these events are lost.

import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;


public class Main {

    protected Shell shell;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            Main window = new Main();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");
        shell.setLayout(new GridLayout(3, false));

        List list = new List(shell, SWT.BORDER);
        list.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        list.add("Something");

        Composite composite = new Composite(shell, SWT.EMBEDDED);
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));
        Frame graphFrame = SWT_AWT.new_Frame(composite);
        final JTextArea textarea = new JTextArea(60, 80);
        JScrollPane scrollpane = new JScrollPane(textarea);
        graphFrame.add(scrollpane);

        textarea.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent arg0) {
                textarea.setText(textarea.getText() + "Mouse clicked.\n\r");
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                textarea.setText(textarea.getText() + "Mouse is in.\n\r");
            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }

        });

        textarea.addMouseWheelListener(new MouseWheelListener() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent arg0) {
                textarea.setText(textarea.getText() + "Mouse wheel activated.\n\r");
            }

        });
    }
}

我发现,Panel能够捕获awt事件,但是任何扩展JComponent的类(就像我的组件一样)都会以某种方式释放这些事件.我还设法创建了一个丑陋的hack,在我单击JComponent之后,在我的swt复合材料上强行聚焦.然后,我至少可以侦听swt事件,并直接从swt事件侦听器使用JComponent.我仍然很欣赏能够直接在JComponent上捕获awt鼠标事件的解决方案.

I found out, that Panel is able to catch awt events, however any class, that extends JComponent (just like my component do) somehow loose these events.I also managed to create an ugly hack, where I forceFocus on my swt composite after I click on my JComponent. Then I can at least listen to swt events and work with my JComponent directly from swt event listener. I would still aprreciate the solution where I will be able to catch awt mouse events directly on JComponent.

推荐答案

您可能会遇到此问题: http://code.google.com/p/gama-platform/issues/detail?id=84 . http://code.google上介绍了一种解决方法. com/p/gama-platform/issues/detail?id = 84#c13 (与此问题相关联).

You may be running into this issue: http://code.google.com/p/gama-platform/issues/detail?id=84. A workaround is described in http://code.google.com/p/gama-platform/issues/detail?id=84#c13 (linked to this very question).

这篇关于MouseWheel事件不会在SWT-AWT组件中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:32