MouseWheel 事件在 SWT-AWT 组件中不触发

发布于 2024-12-07 00:48:24 字数 3491 浏览 0 评论 0原文

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

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

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 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");
            }

        });
    }
}

EDIT: 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文